2011-02-10 103 views
1

正如我已經創建了一個基於Excel的調度程序的一部分,我使用以下方式將「Application.Ontime」功能在指定的時間如何將Application.Run用於受密碼保護的工作簿?

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從密碼保護的工作簿運行宏?

回答

0

由於工作簿受密碼保護,openign的文件時,密碼總是被提供。爲了克服這個問題,我需要將密碼存儲在我的工作簿中。所以這個問題實際上歸結爲用正確的密碼打開文件。

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 
1

它沒有workie。 MS不支持它。運行之前,您需要取消保護工作簿。這裏有一個例子:http://www.ozgrid.com/forum/showthread.php?t=36816&page=1

+0

不是我想要的解決方案,鏈接描述瞭如何保護和取消保護工作表。我的請求與passworf保護的工作簿有關,即用戶需要在openign文件時提供密碼。不過,這是一個很好的鏈接,它幫助我解決了一個無關的問題。 – Ahmad 2011-02-18 05:15:26