如何要求excel 2010工作簿在特定的文件path中以打开VBA?

我想要求工作簿位于打开的特定文件path中。 我在Excel 2010中使用VBA。

(Project / ThisWorkbook code:Header:Workbook / Open)

Private Sub Workbook_Open FilePath 'Runs file path check when workbook opened End Sub 

(项目/模块/模块1标题:(一般)FilePath)

 Sub FilePath() If ActiveWorkbook.Path = ("path address") Then PathOk Else MsgBox ("This is an Unauthorized copy of this file. Please contact Administrator"), vbOKOnly ActiveWindow.Close SaveChanges:=False Exit Sub End If End Sub 

问题是工作簿当前打开并运行PathOk routine加class无论文件path。

为了确保你正在检查正确的工作簿,也许你应该这样做:

在“Thisworkbook”代码模块中:

 Private Sub Workbook_Open() FilePath Me End Sub 

在一个标准的代码模块中:

 Sub FilePath(WB As Workbook) If WB.Path = ("path address") Then PathOk Else MsgBox "This is an Unauthorized copy of this file. Please contact Administrator", _ vbOKOnly + vbCritical 'So it looks more critical ;) ActiveWindow.Close SaveChanges:=False Exit Sub End If End Sub