This question already has an answer here:
Runtime Error VBS/VBA
(1 answer)
Closed 6 years ago.
I have a VB script that runs a macro in an excel file.
The code runs and produces the result file correctly.
The Excel Macro runs fine on it's own.
I get an error after the VB code saying 'Unknown runtime error'
'Code 800A9C68'
'Line 8''Char 2'
Code:
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("Z:\Confidential Restricted\Weekly_HR_Employees_Macro.xlsm", False)
'msgbox("3")
xlApp.Run "Weekly_HR_Employees_Macro.Weekly_HR_Employees_Macro"
'xlbook.Save
'xlBook.Close False
'set xlBook = Nothing
xlApp.Quit
'Set xlApp = Nothing
Try the VBScript below:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.Workbooks.Open("Z:\Confidential Restricted\Weekly_HR_Employees_Macro.xlsm",0, False)
xlApp.Run "Weekly_HR_Employees_Macro"
xlBook.Close True
xlApp.Quit
set xlBook = Nothing
Set xlApp = Nothing
Related
The following code, when used within Word VBA, successfully creates a standard Excel workbook in the chosen folder:
Const ExcelSourcePath = "C:\Users\Holge\_Universet i billeder\_ExcelDocs\"
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
Application.DisplayAlerts = False
xlBook.SaveAs FileName:=ExcelSourcePath & "TestFile.xlsx"
Application.DisplayAlerts = True
xlBook.Close False
Set xlBook = Nothing
xlAppl.Quit
Set xlAppl = Nothing
End Sub
However, what I really need is to create a macro-enabled file. But if I change ".xlsx" into ".xlsm" I get a RTE 1004, "Wrong file type name" (translated from Danish).
Perhaps CreateObject should be called with another argument, but which one? I have not been alble to find possible values for this argument.
In the following code:
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
A new Excel Application instance is created twice. You need to keep the one line of code for create a new instance, not doing that twice.
Moreover, an instance of the Workbook class can't be created by using the New operator like shown in your code:
Dim xlBook As New Excel.Workbook
Most probably the intention was to declare objects out of the sub and then instantiate them:
Const ExcelSourcePath = "C:\Users\Holge\_Universet i billeder\_ExcelDocs\"
Dim xlAppl As Excel.Application
Dim xlBook As Excel.Workbook
Private Sub TestCreateExcelFile()
Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Add
Application.DisplayAlerts = False
xlBook.SaveAs FileName:=ExcelSourcePath & "TestFile.xlsx"
Application.DisplayAlerts = True
xlBook.Close False
Set xlBook = Nothing
xlAppl.Quit
Set xlAppl = Nothing
End Sub
Just need to remove New operators from the code with declarations.
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 am receiving an "object required" error while writing a VBScript to run a macro outside of Excel. The code is below this message. After declaring xlApp = CreateObject("Excel.Application"), any attempted action on the xlApp object will throw the "object required" error.
Am I missing something in my declaration statement, or is there some sort of import/include statement I need to make?
Sub temp()
Dim xlApp, xlBook
Dim oShell: Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = "H:"
MsgBox (oShell.CurrentDirectory)
xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'error is thrown here
xlBook = xlApp.Workbooks.Open("H:\SW Tool Resources\test\tester.xlsm")
MsgBox ("Success")
End Sub
You need to Set your object variable to the new instance of Excel:
Set xlApp = CreateObject("Excel.Application")
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