打开Excel并传递文件密码

时间:2014-12-05 19:46:19

标签: excel vbscript

我有一些我每晚刷新的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

任何帮助?我真的不使用vb​​s(根本没有)

编辑,此代码可以打开文件刷新所有,保存然后关闭,我只是不知道如何让它输入密码以便在完全编辑模式下打开它

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

2 个答案:

答案 0 :(得分: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/...

答案 1 :(得分:0)

在VBA中:

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

或者在你的情况下:

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