如何使用Application.Run密码保护的工作簿?

作为我创build的基于Excel的调度程序的一部分,我使用以下方式的“Application.Ontime”函数在指定的时间执行macros

Sub Load //snipped for brevity Dim startName As String startName = "'StartSub""" & filePath & """, """ & fileName & """, """ & macroName & """'" Application.OnTime y, startName End Sub Sub StartSub(filePath As String, fileName As String, macroName As String) Dim wb As String wb = "'" & filePath & "'!" & macroName Application.Run wb Application.Workbooks(fileName).Close Savechanges:=True End Sub 

testing和简单的POC似乎工作得很好。 我面临的问题是其中一个电子表格是密码保护的。 问题是密码input对话框阻止macros执行我猜测是预期的。 由于输出和结果被导出到多个报告,所以不需要对密码保护的工作簿进行写入访问。

我的问题是如何克服这一点,并使用Application.Run从密码保护工作簿运行macros?

它没有工作。 MS不支持它。 在运行之前,您需要取消保护工作簿。 这是一个例子: http : //www.ozgrid.com/forum/showthread.php?t=36816&page=1

由于工作簿受密码保护,因此在打开文件时始终提供密码。 为了解决这个问题,我需要将密码存储在我的工作簿中。 所以这个问题实际上是用正确的密码打开一个文件。

 Sub StartSub(filePath As String, fileName As String, macroName As String) Dim wb As String wb = "'" & filePath & "'!" & macroName Dim scheduledWb As Workbook Dim pwd As String Dim saveChange As Boolean pwd = GetPassword(fileName) ' method that extracts correct password from somewhere If Len(pwd) > 0 Then Set scheduledWb = Workbooks.Open(fileName:=filePath, ReadOnly:=True, Password:=pwd) saveChange = False Else Set scheduledWb = Workbooks.Open(fileName:=filePath) saveChange = True End If Application.Run "'" & fileName & "'!" & macroName Application.Workbooks(fileName).Close Savechanges:=saveChange End Sub