2014-12-05 76 views
0

我有一些出類拔萃,我刷新夜間打開Excel,並通過文件密碼通過

我現在想用密碼保護它們,以從中修改任何數據文件,但是當腳本打開文件時,它會提示輸入密碼,

我如何傳遞密碼與腳本?

繼承人我使用(嘗試)

Set fs = CreateObject("Scripting.FileSystemObject") 
Set rootFolder = fs.GetFolder(fs.GetParentFolderName(wscript.ScriptFullName)) 
Set oExcel = CreateObject("Excel.Application") 

oExcel.Visible = True 
oExcel.DisplayAlerts = False 
oExcel.AskToUpdateLinks = False 
oExcel.AlertBeforeOverwriting = False 

For Each file in rootFolder.Files 
    If inStr(file.type, "Script") = 0 Then 
    Set oWorkbook = oExcel.Workbooks.Open(file.path) 

Dim WSH 
Set WSH = CreateObject("WScript.Shell") 
wsh.sleep(25000) 
Wsh.SendKeys "?" 
Wsh.SendKeys "{ENTER}" 

    oWorkbook.RefreshAll 
    oWorkbook.Save 
    oWorkbook.Close 
    Set oWorkbook = Nothing 
    End If 
Next 

oExcel.Quit 
set oExcel = nothing 

任何幫助嗎?我沒有真正使用vbs很多(根本沒有)

編輯,此代碼在打開文件的工作刷新所有,保存然後關閉,我只是不知道如何得到它輸入密碼以便全面打開它編輯模式

Set fs = CreateObject("Scripting.FileSystemObject") 
Set rootFolder = fs.GetFolder(fs.GetParentFolderName(wscript.ScriptFullName)) 
Set oExcel = CreateObject("Excel.Application") 

oExcel.Visible = True 
oExcel.DisplayAlerts = False 
oExcel.AskToUpdateLinks = False 
oExcel.AlertBeforeOverwriting = False 


For Each file in rootFolder.Files 
    If inStr(file.type, "Script") = 0 Then 
    Set oWorkbook = oExcel.Workbooks.Open(file.Path) 

    oWorkbook.RefreshAll 
    oWorkbook.Save 
    oWorkbook.Close 
    Set oWorkbook = Nothing 
    End If 
Next 

oExcel.Quit 
set oExcel = nothing 

回答

4

這不是我清楚,但除了由@wahwahwah這裏的建議是其他的解釋:

Set oWorkbook = oExcel.Workbooks.Open(Filename:=file.Path, Password:="<myPassword") 

編輯

Set oWorkbook = oExcel.Workbooks.Open(Filename:=filePath, Password:="<myPassword") 

編輯2

進一步討論和澄清確定了這在VBScript語法似乎工作!

Set oWorkbook = oExcel.Workbooks.Open(file.Path,,,, "mypassword",,,,,,,,,,)適合的參數。

this link給了靈感!

msdn.microsoft.com/en-us/...

+0

它沒有工作,讓 – lookslikeanevo 2014-12-05 20:20:01

+1

@lookslikeanevo「語句的預期結束」的錯誤 - apols,錯過了括號,回答編輯 – barryleajo 2014-12-05 20:30:39

+0

現在變得「預期」)'「 – lookslikeanevo 2014-12-05 20:38:02

0

在VBA:

ActiveSheet.Unprotect Password:="password" 
ActiveWorkbook.Unprotect Password:="password" 

或者你的情況:

oExcel.Workbooks.Application.ActiveWorkbook.Unprotect Password = "password" 
+0

coudlnt得到這個工作,無論是 – lookslikeanevo 2014-12-05 20:23:39