I have the code shown below, which opens a workbook and runs the corresponding macro. Running this script through excel works just fine, but running the .vbs file with the same code does not run the macro. I have tried both double clicking the file and running it through the cmd prompt with "cscript.exe LaunchMacro.vbs". In addition, using WScript.Echo does not print to my command line. Am i missing something here?
Thank you!
Option Explicit
Sub LaunchMacro()
Dim xlApp, xlBook
Dim oShell: Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = "H:"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Application.Visible = False
Set xlBook = xlApp.Workbooks.Open("H:\SW Tool Resources\test\tester.xlsm")
MsgBox ("File Opened")
xlApp.DisplayAlerts = False
xlApp.Application.Run ("tester.xlsm!Module3.split")
MsgBox ("Application Should Have Run")
xlBook.Saved = True
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
VBS and VBA are different.
Try to put only this code in the vbs file and run it:
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Application.Visible = False
Set xlBook = xlApp.Workbooks.Open("H:\SW Tool Resources\test\tester.xlsm")
MsgBox ("File Opened")
xlApp.DisplayAlerts = False
xlApp.Application.Run ("tester.xlsm!Module3.split")
MsgBox ("Application Should Have Run")
xlBook.Saved = True
xlApp.Quit
I didn't test it, and very likely will not succeed. But it should put it in the right direction.
Edit:
Here is something that should help you getting started with VBS: a script can define some functions, but only runs them if you call them.
For example this will only run Sub1:
Sub Sub1()
MsgBox "1"
End Sub
Sub Sub2()
MsgBox "2"
End Sub
Sub1
Related
Trying to run excel macro using command prompt through VBS. Excel file open ups but not able to run macro. The name of Macro as well as module is "Wallet"
While tryig to view macro, it reflects as "PERSONAL.XLSB!WALLET"
Pls help
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("excel.application")
Set xlBook = xlApp.Workbooks.Open("C:\Output\abc.xlsb")
xlApp.Visible = True
xlApp.Run "Wallet"
xlApp.Save
xlApp.ActiveWorkbook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Change your code like that
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("excel.application")
Set xlBook = xlApp.Workbooks.Open("C:\Output\abc.xlsb")
xlApp.Visible = True
xlApp.Workbooks.Open ("Path to your Personal.XLSB")
xlApp.Run "Personal.XLSB!Macro"
'xlApp.Save <= This line would not work, xlApp does not have a Save Method
'xlApp.ActiveWorkbook.Close <= You already have the reference to the workbook. It is xlBook
xlBook.Close True ' <= This will save and close the workbook you opened
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
If you do not know where your Personal.xlsb is try the standard location.
xlApp.Workbooks.Open (xlApp.StartupPath & xlApp.PathSeparator & "Personal.XLSB")
I am trying to print a file from a vbs script, and I am getting a subscript out of range. The macro works great when ran from excel.
The VBS script is:
dim xlApp
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Run "'C:\Users\jporter8\Documents\dashboard\testing.xlsm'!Module1.print_Test"
xlApp.DisplayAlerts = True
xlApp.Application.quit
set xlApp = Nothing
and the excel macro is:
Sub print_Test()
Sheets("two").PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
MsgBox "Print successful"
End Sub
And the sheet I am trying to print is named "two"
I found this work around to open the workbook so that the sheet existed and wasn't out of range
dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.Workbooks.open("C:\Users\jporter8\Documents\dashboard\testing.xlsm", 0, true)
xlApp.Run "Module1.print_Test"
xlApp.DisplayAlerts = True
wscript.echo "Printed"
xlBook.Close
xlApp.Quit
set xlApp = Nothing
set xlBook = Nothing
wscript.echo "Finished"
wscript.Quit
I'm a newbie with vbs and trying to figure out how to reopen excel file.
I have like 100+ excels in one folder, where I need to open them one by one and run the macro.
It is working pretty well except for files, which are already open. I would like to reopen them and edit them.
Could you please advise how to do it?
Bellow is the code:
Const sRootFolder = "C:\test"
Dim oFSO, oFile ' File and Folder variables
Dim xlApp, xlBook, objWorkbook
Start
Sub Start()
Initialize
ProcessFilesInFolder sRootFolder
Finish
End Sub
Sub ProcessFilesInFolder(sFolder)
' Process the files in this folder
For Each oFile In oFSO.GetFolder(sFolder).Files
If IsExcelFile(oFile) Then ProcessExcelFile oFile.Path
Next
End Sub
Sub Initialize()
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set xlApp = CreateObject("Excel.Application")
End Sub
Sub Finish()
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Set oFSO = Nothing
End Sub
Function IsExcelFile(oFile)
IsExcelFile = (InStr(1, oFSO.GetExtensionName(oFile), "xls", vbTextCompare) > 0) And (Left(oFile.Name, 1) <> "~")
End Function
Sub ProcessExcelFile(sFileName)
Set xlBook = xlApp.Workbooks.Open(sFileName, 0, True)
Set objWorkbook = xlApp.Workbooks.Open(sFileName)
xlApp.Run "macro"
End Sub
Sub Save()
xlBook.Save
xlBook.Close
xlApp.Quit
WScript.Echo "Finished."
End Sub
I would like to run a VBA macro named MyMacro, which is saved as MyMacro.bas for many excel files. I have the VBS code below, but it is not doing what I want. I would really appreciate if somebody could take a look at it.
I am using Excel 2013. The files are saved as .xls.
Thank you.
Const sRootFolder = "C:\Documents"
Const sExportedModule = "C:\Documents\MyMacro.bas"
Const sMacroName = "MyMacro"
Dim oFSO, oFDR, oFile ' File and Folder variables
Dim oExcel, oWB ' Excel variables (Application and Workbook)
Start
'------------------------------
Sub Start()
Initialize
ProcessFilesInFolder sRootFolder
Finish
End Sub
'------------------------------
Sub ProcessFilesInFolder(sFolder)
' Process the files in this folder
For Each oFile In oFSO.GetFolder(sFolder).Files
If IsExcelFile(oFile) Then ProcessExcelFile oFile.Path
Next
End Sub
'------------------------------
Sub Initialize()
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
End Sub
'------------------------------
Sub Finish()
oExcel.Quit
Set oExcel = Nothing
Set oFSO = Nothing
End Sub
'------------------------------
Function IsExcelFile(oFile)
IsExcelFile = (InStr(1, oFSO.GetExtensionName(oFile), "xls", vbTextCompare) > 0) And (Left(oFile.Name, 1) <> "~")
End Function
'------------------------------
Sub ProcessExcelFile(sFileName)
On Error Resume Next
wscript.echo "Processing file: " & sFileName ' Comment this unless using cscript in command prompt
Set oWB = oExcel.Workbooks.Open(sFileName)
oWB.VBProject.VBComponents.Import sExportedModule
oExcel.Run sMacroName
oWB.Save
oWB.Close
Set oWB = Nothing
End Sub
'------------------------------
Here is a vbs code for a single file which works:
Option Explicit
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Dim objWorkbook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Documents\test.xls", 0, True)
Set objWorkbook = xlApp.Workbooks.Open("C:\Documents\test.xls")
xlApp.Run "MyMacro"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
I finally got it working:
Const sRootFolder = "C:\Documents"
Const sExportedModule = "C:\Documents\MyMacro.bas"
Const sMacroName = "Trip"
Dim oFSO, oFile ' File and Folder variables
Dim xlApp, xlBook, objWorkbook
Start
Sub Start()
Initialize
ProcessFilesInFolder sRootFolder
Finish
End Sub
Sub ProcessFilesInFolder(sFolder)
' Process the files in this folder
For Each oFile In oFSO.GetFolder(sFolder).Files
If IsExcelFile(oFile) Then ProcessExcelFile oFile.Path
Next
End Sub
Sub Initialize()
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set xlApp = CreateObject("Excel.Application")
End Sub
Sub Finish()
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Set oFSO = Nothing
End Sub
Function IsExcelFile(oFile)
IsExcelFile = (InStr(1, oFSO.GetExtensionName(oFile), "xls", vbTextCompare) > 0) And (Left(oFile.Name, 1) <> "~")
End Function
Sub ProcessExcelFile(sFileName)
wscript.echo "Processing file: " & sFileName ' Comment this unless using cscript in command prompt
Set xlBook = xlApp.Workbooks.Open(sFileName, 0, True)
Set objWorkbook = xlApp.Workbooks.Open(sFileName)
objWorkbook.VBProject.VBComponents.Import sExportedModule
xlApp.Run sMacroName
End Sub
Also, make sure that Trust access to the VBA project object model enabled. I certainly may be wrong, but the game changer here seems to be this piece:
Set objWorkbook = xlApp.Workbooks.Open(sFileName)
I know there are a lot of posts which say they are new to VBS, but honestly, I have never written VBS code before....
What I am trying to automate is a report and I have managed to cobble together the following:
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open ("C:\Users\pierre\Desktop\Test folder\16 10 19 Spreadsheet.xlsm", 0, True)
xlApp.Run "macro1"
xlbook.Save
xlBook.Close False
set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
Obviously it just opens an excel workbook at a specific location, runs a macro and ends, but what I dont understand is how to account for a dymanic filename in the code eg the date is going to change, but nothing else?
Is there a wildcard variable that can be used?
Any help please!
You can just store the date to a variable and use that in your filename:
Dim xlApp, xlBook
Dim formattedDate
formattedDate = Right(datepart("yy", Now), 2) & " " & DatePart("m", Now) & " " & DatePart("d", Now)
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open ("C:\Users\pierre\Desktop\Test folder\" & formattedDate & " Spreadsheet.xlsm", 0, True)
xlApp.Run "macro1"
xlbook.Save
xlBook.Close False
set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
Here we use function datepart to get pieces of the current date Now. We also make use of function Right() which will get the right-most number of characters specified for the string in the first parameter. All of that is concatenated together using the & concatenation operator. Then with that formattedDate we just concatenate it right into the filename.