Vba 如何使用应用程序。为受密码保护的工作簿运行?
作为我创建的基于Excel的计划程序的一部分,我正在以以下方式使用“Application.Ontime”函数在指定时间执行宏Vba 如何使用应用程序。为受密码保护的工作簿运行?,vba,excel,Vba,Excel,作为我创建的基于Excel的计划程序的一部分,我正在以以下方式使用“Application.Ontime”函数在指定时间执行宏 Sub Load //snipped for brevity Dim startName As String startName = "'StartSub""" & filePath & """, """ & fileName & """, """ & macroName & """'"
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
测试和简单的POC似乎工作得非常好。我面临的问题是其中一个电子表格有密码保护。问题是密码输入对话框阻止宏执行,我猜这是预期的。不需要对受密码保护的工作簿进行写访问,因为输出和结果会导出到多个报表中
我的问题是如何克服这个问题,并使用Application.run从受密码保护的工作簿运行宏?It no workie。MS不支持它。您需要在运行工作簿之前解除对其的保护。这里有一个例子:它不工作。MS不支持它。您需要在运行工作簿之前解除对其的保护。下面是一个示例:由于工作簿受密码保护,因此在打开文件时始终提供密码。为了克服这个问题,我需要在工作簿中存储密码。因此,问题实际上归结为使用正确的密码打开文件
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
由于工作簿受密码保护,因此在打开文件时始终提供密码。为了克服这个问题,我需要在工作簿中存储密码。因此,问题实际上归结为使用正确的密码打开文件
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
这个链接并不是我想要的解决方案,它描述了如何保护和取消保护工作表。我的请求与passworf保护的工作簿有关,即用户需要在打开文件时提供密码。尽管如此,还是有一个很好的链接帮助我解决了一个不相关的问题。这个链接并不是我想要的解决方案,它描述了如何保护和取消保护工作表。我的请求与passworf保护的工作簿有关,即用户需要在打开文件时提供密码。然而,一个很好的链接帮助我解决了一个无关的问题。