2014-04-02 48 views
2

我想要求工作簿位於打開的特定文件路徑中。如何要求excel 2010工作簿在特定的文件路徑中以便用VBA打開?

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

(項目/模塊/模塊1 頭:(一般)文件路徑)

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 
我在Excel 2010中

(: 頭工作簿/打開項目/的ThisWorkbook代碼)使用VBA

問題是工作簿當前打開並運行PathOk routine加班,無論文件路徑如何。

+0

您的代碼適用於我的目的。你會遇到什麼情景? – L42

回答

2

要確保你正在檢查正確的工作簿也許你應該做這樣的:

在「的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 
相關問題