2015-09-22 73 views
0

我遇到問題。我想實現宏,當用戶無權訪問文件時,它會轉到錯誤處理程序,但err.description不起作用,因爲當我想打開適當的文件,必須插入登錄名和密碼的窗口,以及此處是想法,當這個意外的窗口顯示宏應該去錯誤處理程序,但我不怎麼實現它,下面的代碼將顯示提到的窗口。當文件打開時避免打開另一個窗口

Application.DisplayAlerts = False 
On Error GoTo AccessProblem 
Workbooks.Open filename=:"link to file" 
'here login window is displayed 
AccessProblem: 
MsgBox err.Description 
'If InStr(err.Description, "access") <> 0 Then 
' Range(somerange).Interior.Color = RGB(255, 255, 0) 
'End If 

回答

0
Application.DisplayAlerts = False 
On Error GoTo AccessProblem 
Application.DisplayAlerts = False 
Workbooks.Open filename=:"link to file", password ="Not your password" 
Application.DisplayAlerts = True 
'here login window is displayed 
AccessProblem: 
MsgBox err.Description 
'If InStr(err.Description, "access") <> 0 Then 
' Range(somerange).Interior.Color = RGB(255, 255, 0) 
'End If 

這是你需要做的事情。在您嘗試打開的階段禁用警報 - 因此它不會以錯誤的方式轟擊您,然後放入一個gobbledigook密碼,以便它不會打開。這將觸發錯誤處理程序。

+0

但我確信文件存在,我想有可能檢查文件是否立即打開,沒有任何其他窗口 – Maciej

+0

我不知道我遵循 - 但你可以看看工作簿打開命令在這個工作簿對象可能 - 如果你想觸發一些東西被打開。 – Trum

+0

如果該文件沒有打開,因爲訪問警告窗口,我必須在其中插入登錄名和密碼,我希望宏跳過這個文件 – Maciej

相關問題