使用VBscript从不同的Excel文件运行VBA脚本

我使用这个代码来search一个文件夹,find所有的excel文件(具有相同的扩展名),从打开的excel文件运行一个VBA脚本,并保存它没有提示。

strPath = "my path" pathName="xlsx" if strPath = "" then Wscript.quit if pathName = "" then Wscript.quit Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.DisplayAlerts = False Set objFso = CreateObject("Scripting.FileSystemObject") Set objFolder = objFso.GetFolder (strPath) For Each objFile In objFolder.Files If objFso.GetExtensionName (objFile.Path) = "xlsx" Then Set objWorkbook = objExcel.Workbooks.Open(objFile.Path) Set objWorksheet = objWorkbook.WorkSheets(1) objworksheet.Activate objExcel.Application.Run "'filename and in quote because there is space.xlsm'!TestingMacro" objWorkbook.saveas(objFile.Path) objWorkbook.Close True 'Save changes End If Next objExcel.Quit 

但是,每次我运行它,它只是给我一个线上objExcel.Application.Run运行时错误800A03EC。 所以我可以解决这个问题吗?

谢谢!

包含macros的工作簿必须先打开,然后才能运行macros。 以完整path打开macros工作簿,但仅使用工作簿和macros名称运行macros。

 Set xl = CreateObject("Excel.Application") xl.Visible = True Set wbm = xl.Workbooks.Open("C:\path\to\macro workbook.xlsm") Set fso = CreateObject("Scripting.FileSystemObject") For Each f In fso.GetFolder("C:\some\where").Files If LCase(fso.GetExtensionName(f.Name)) = "xlsx" Then Set wb = xl.Workbooks.Open(f.Path) Set ws = wb.Sheets(1) ws.Activate xl.Application.Run "'macro workbook.xlsm'!TestingMacro" wb.Save wb.Close End If Next wbm.Close xl.Quit 

您试图从您的个人工作簿运行macros可能无法正常工作,因为用VBScript打开Excel文件不会自动打开您的PERSONAL.XLSB。 你将需要做这样的事情:

 Dim oFSO Dim oShell, oExcel, oFile, oSheet Set oFSO = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("WScript.Shell") Set oExcel = CreateObject("Excel.Application") Set wb2 = oExcel.Workbooks.Open("C:\..\PERSONAL.XLSB") 'Specify foldername here oExcel.DisplayAlerts = False For Each oFile In oFSO.GetFolder("C:\Location\").Files If LCase(oFSO.GetExtensionName(oFile)) = "xlsx" Then With oExcel.Workbooks.Open(oFile, 0, True, , , , True, , , , False, , False) oExcel.Run wb2.Name & "!modForm" For Each oSheet In .Worksheets oSheet.SaveAs "C:\test\" & oFile.Name & "." & oSheet.Name & ".txt", 6 Next .Close False, , False End With End If Next oExcel.Quit oShell.Popup "Conversion complete", 10 

所以在循环的开始,它打开personals.xlsb并从那里运行所有其他工作簿的macros。 只是以为我应该在这里张贴,以防万一有人跑过这个像我一样,但无法弄清楚为什么macros仍然没有运行。

您可能需要在excel的新实例的objFolder目录中运行每个excel文件。

 strPath = "my path" pathName="xlsx" if strPath = "" then Wscript.quit if pathName = "" then Wscript.quit Set objFso = CreateObject("Scripting.FileSystemObject") Set objFolder = objFso.GetFolder (strPath) For Each objFile In objFolder.Files If objFso.GetExtensionName (objFile.Path) = "xlsx" Then Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.DisplayAlerts = False Set objWorkbook = objExcel.Workbooks.Open(objFile.Path) Set objWorksheet = objWorkbook.WorkSheets(1) objworksheet.Activate objExcel.Application.Run "'filename and in quote because there is space.xlsm'!TestingMacro" objWorkbook.saveas(objFile.Path) objWorkbook.Close True 'Save changes objExcel.Quit End If Next