I'm trying to create and run a VBScript to run a macro I've created (PVT_Paste_Macro.xlsm) on Excel files without opening each individual Excel file (I have 528 files!) Here is my code:
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\Owner\Desktop\PVT_Paste_Macro.xlsm'!Module1.PVT_Paste_Macro"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
After running the code, I get the following error:
Script: C:\Users\Owner\Desktop\VBA for macros - PVT.vbs
Line: 2
Char: 1
Error: Unknown runtime error
Code: 800A03EC
Source: Microsoft VBScript runtime error
I run the script while the macro file is open. The macro file, the (test) Excel file I'd like to run the macro on, and the VBS script are all located on the Desktop.
Can anyone help? Thanks.
The code does not work, because it does not like the fact, that the excel file, containing the code open before the code is executed:
Using your code, try this simple steps and make sure it works:
Script.xlsm
file on the Desktop. Modul1
: Sub FromExcel()
MsgBox "FROM EXCEL"
End Sub
Script.vbs
like this: Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\UserName\Desktop\Script.xlsm'!Modul1.FromExcel"
objExcel.Visible = True 'Or "False", depending on what you want.
objExcel.Application.Quit
Set objExcel = Nothing
As a way around , if you want to run the "macro" from an opened Excel file, then something like GetObject would be useful:
Set objExcel = GetObject("C:\Users\UserName\Desktop\xl.xlsm")
objExcel.Application.Run "Modul1.FromExcel"
objExcel.Application.Quit
Set objExcel = Nothing
However, this is probably an XY problem - there should be a better way of doing the whole story, eg running the "macro" from a single independent Excel file, which opens the other Excel files in the desktop and processes them, without the need of VBScript. Probably check these:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.