打开Excel并通过文件密码

我有一些我每晚刷新的excel文件

我现在要密码保护他们,以便修改他们的任何数据,但是当脚本打开文件时,它会提示input密码,

我如何通过脚本密码?

inheritance人我用什么(已经尝试过)

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很多(根本)

编辑,这个代码工作在打开文件刷新所有,保存然后closures,我只是不知道如何得到它input密码以打开它在全编辑模式

 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 

我不清楚,除了@Wahwahwah的build议外,还有另一种解释:

 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",,,,,,,,,,)参数来适应。

这个链接给了灵感!

msdn.microsoft.com/en-us/…

在VBA中:

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

或者在你的情况下:

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