VB脚本打开文件夹中的所有Excel文件并运行其中的宏

时间:2015-01-28 18:52:30

标签: excel vbscript

我正在尝试打开一个VB脚本,打开给定文件夹中的每个Excel文件,运行写在其中的宏(宏写为Workbook_Open(),因此它会自动执行)并关闭文件

目前,我已将此代码粘贴在下方,但它无效。它只显示已在宏上打开的消息,但对工作簿没有任何作用。

OpenAllFiles()
MsgBox "Completed opening all Excel files in the folder."

Sub OpenAllFiles()

    dim fso
    dim thisfolder
    dim allfiles

    Set WshShell = WScript.CreateObject("WScript.Shell")
    set fso= Wscript.CreateObject("Scripting.FileSystemObject")
    set thisfolder = fso.GetFolder("C:\Users\ad6975\Desktop\Project\02 2014 IC\04 IC4\02 Goal Reporting\Test")
    set allfiles = thisfolder.Files

    Dim XL
    Set XL = CreateObject("excel.application") 

    For each onefile in allfiles
        if (onefile.type = "Microsoft Office Excel 97-2003 Worksheet" Or onefile.type = "Microsoft Excel Worksheet") then

            Dim XLWkbk
            Set XLWkbk = XL.Workbooks.Open(onefile)
            XL.DisplayAlerts = False
            Call XLWkbk.SaveAs(onefile.Path, 56, , , , , ,2)
            XLWkbk.Close
            Set XLWkbk = Nothing

        end if
    Next

    XL.Quit
    Set XL = Nothing 

End Sub

0 个答案:

没有答案