关闭时打开文件密码提示

Open File Password Prompt While Closing

我有两个带有宏的独立工作簿:带有 Macro1 的 Book1 和带有 Macro2 的 Book2。 Macro1 是简单的代码:

Sub Macro1()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False

Dim myWb As Workbook
Set myWb = Workbooks.Open(ThisWorkbook.Path & "/" & "Book2.xlsb", True, False, , "1111")
myWb.Unprotect ("1111")


Cells(1, 1) = "test"

myWb.IsAddin = True
Application.Run "'Book2.xlsb'!Macro2"
myWb.Close savechanges:=True


Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Sub Macro2()
MsgBox "Hello! I am an msgbox from Book2"
End Sub

我强调一切都应该秘密进行,用户不应该看到 Book2 和任何闪光灯等。 但是当代码进入保存步骤时,屏幕上会弹出一个 window,顺便说一句,可以忽略它,代码将继续 run/execute 但无论如何这是一个我想摆脱的问题: password pop-up window

我注意到的:

V1: myWb.IsAddin = 正确的是 deleted/commented

Application.Run "'Book2.xlsb'!Macro2"
myWb.Close savechanges:=True

V2: ON/OFF IsAddin = True before/after Macro2 正在执行

myWb.IsAddin = True
Application.Run "'Book2.xlsb'!Macro2"
myWb.IsAddin = False
myWb.Close savechanges:=True

请尝试下一种方法。它将在新会话中打开工作簿,将在其第一个 sheet 和 运行 中写入宏。但是从不可见的 session/window 发送消息的宏并不是最好的主意。要查看消息,请将光标移到任务栏上的 Excel 工作簿图标上,select MsgBox window,然后按 OK。一个更好的主意是避免发送消息。代码应 运行 根据需要:

Sub HiddenWBOpenRunMacro()
  Dim newEx As Excel.Application, myWb As Workbook
  
  Set newEx = CreateObject("Excel.Application")
  'Open the workbook in the newly created session:
  Set myWb = newEx.Workbooks.Open(ThisWorkbook.Path & "/" & "Book2.xlsb", , , , "1111")

  myWb.Sheets(1).cells(1, 1).Value = "Test"  'write in its first sheet
  myWb.Application.Run "'Book2.xlsb'!Macro2" 'run the macro

  myWb.Close True                            'close the workbook, saving it
  newEx.Quit                                 'quit the session
End Sub

如果 ThisWorkbook 不在 Excel Trusted Locations 中,则必须添加它。