Excel VBA:如何在宏中关闭链接文件的密码框

时间:2018-01-08 12:05:02

标签: excel vba excel-vba password-protection

我正在尝试在我的宏运行时解除弹出密码框,每个文件都受密码保护,我有解锁这些文件的代码,但文件也链接到excel提示我输入密码的其他受密码保护的文件,每次弹出其中一个方框时,有没有办法解除宏中的密码框,而不是点击取消?

这是我目前的代码:

Sub OpenCurrentGBP()

cdirectory = Range("E5").Value
Mdirectory = Range("E6").Value

cGap = Range("E11").Value
cEVE = Range("E12").Value
cHedge = Range("E13").Value
cVarFile = Range("E16").Value
cGapMovements = Range("E17").Value
cQRMCheck = Range("E18").Value

GapPwd = Range("E42").Value
EVEPwd = Range("E43").Value
HedgePwd = Range("E44").Value
EurogapPwd = Range("E45").Value
EuroEVEPwd = Range("E46").Value
VarPwd = Range("E47").Value
MovPwd = Range("E48").Value

Application.DisplayAlerts = False

Call OpenFile(cdirectory, cGapMovements, MovPwd)
Call OpenFile(cdirectory, cGap, GapPwd)
Call OpenFile(cdirectory, cEVE, EVEPwd)
Call OpenFile(cdirectory, cHedge, HedgePwd)
Call OpenFile(cdirectory, cVarFile, VarPwd)
Call OpenFile(cdirectory, cQRMCheck)

Application.DisplayAlerts = True

End Sub

OpenFile宏如下:

Sub OpenFile(Directory, File, Optional Pass)
On Error GoTo Failure

Application.DisplayAlerts = False
Application.AskToUpdateLinks = False

If IsMissing(Pass) = 0 Then

Workbooks.Open Filename:= _
Directory & "\" & File, Notify:=False, Password:=Pass
Else
Workbooks.Open Filename:= _
Directory & "\" & File, Notify:=False
End If

Application.DisplayAlerts = True
Application.AskToUpdateLinks = True

Exit Sub
Failure:     MsgBox (File & " could not be opened")

Application.DisplayAlerts = True
Application.AskToUpdateLinks = True

End Sub

1 个答案:

答案 0 :(得分:0)

在潜艇里面你call(下次你也应该在这里展示)打开受保护的工作簿,你需要使用Workbooks.open声明,我有一个我自己的代码的例子,我是用于打开带密码的工作簿:

Workbooks.Open Filename:=tmp_file_p, Password:="7399"

tmp_file_p是一个字符串变量,包含工作簿的路径和名称。

相关问题