VBScript that runs a macro in excel throws an error - excel

I have a vbscript that opens an excel workbook and runs a particular macro inside at a particular time on a particular day. Here it is
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
While not (Month(Now()) = 11 and Day(Now()) = 17 and Hour(Now()) = 9 and Minute(Now()) = 1)
Wend
Set xlBook = xlApp.Workbooks.Open("C:\Temp\Property by Peril.xlsm")
xlApp.Application.Run "'Property by Peril.xlsm'!Main"
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
It runs fine up to and including the opening of the workbook and the running of the macro. However, immediately after the macro runs, I get an error
Microsoft VBScript runtim error: Unknown runtime error (on line 16)
and the rest of the script does not get executed.
Any thoughts/help?

Try removing the statement to close the workbook from the macro and adding it to the VBScript. Also, don't forget to quit the application object, otherwise your hidden Excel instance will keep lingering in memory, because it's not automatically discarded when the script terminates.
...
xlApp.Application.Run "'Property by Peril.xlsm'!Main"
xlBook.Close False
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
...
The parameter False will prevent changes to the workbook from being saved. Change it to True if you want changes to be automatically saved.

The following code ran successfully on my system.
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:\Temp\Property by Peril.xlsm")
xlApp.Application.Run "Main"
xlBook.Close False
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit

Related

Opening Excel file located in one drive

I need to open the excel file located in share point using Excel.Application
In Visual Basic 6, I am using following to open the excel file located in Sharepoint:
Shell "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE https://livecnm-my.sharepoint.com/:x:/g/personal/sghimire_cnm_edu/EZwyv4Ot1_xEnIeT1SIADncBVtRQ0b6THoJj0eLrbpEjXQ?e=NgyEBD", vbNormalFocus
But i want to use Excel.Application instead of shell so, i can hide ribbon, status bar, formula bar and other stuff before excel file open.I have tried following but it not working:
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Workbooks.Open "https://livecnm-my.sharepoint.com/:x:/g/personal/sghimire_cnm_edu/EZwyv4Ot1_xEnIeT1SIADncBVtRQ0b6THoJj0eLrbpEjXQ?e=NgyEBD"
.Application.ExecuteExcel4Macro "Show.toolbar(""Ribbon"",False)"
.Application.DisplayStatusBar = False
.Application.DisplayFormulaBar = False
.Application.DisplayScrollBars = True
.Application.Visible = True
End With
Maybe you give this a try
Sub open_excel_from_one_drive()
Dim sfilename As String
Dim xl As Excel.Application
Dim xlsheet As Workbook
sfilename = "https://d.docs.live.net/78a58439bd7a4267/Investment%20Spreadsheet/Customer%20Management/Security%20Data.xlsm"
Set xl = Application
Set xlsheet = xl.Workbooks.Open(Filename:=sfilename)
End Sub
If you want to go on using your code then you could try
Sub dummy_Code()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Workbooks.Open "https://d.docs.live.net/78a58439bd7a4267/Investment%20Spreadsheet/Customer%20Management/Security%20Data.xlsm"
.Application.ExecuteExcel4Macro "Show.toolbar(""Ribbon"",False)"
.Application.DisplayStatusBar = False
.Application.DisplayFormulaBar = False
.Application.DisplayScrollBars = True
.Application.Visible = True
End With
End Sub

Getting error on closing excel macro using vbscript

Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C: \Net_Zero_Final.xlsm",0,True)
ObjExcel.Visible = True
ObjExcel.Run "Net_Zero"
ObjExcel.application.quit ' <--- error here
Set ObjWB = Nothing
Set ObjExcel = Nothing
Getting error on the BOLD Part
Can anyone please suggest. tried everything to fix this.
I think you are trying to run a VBA Macro from a .vbs file (or any other location).
I tested the following code inside test.vbs.
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C:\Users\PC\Downloads\TestMacro2.xlsm", False, False)
ObjExcel.Visible = True
ObjExcel.Application.Run "TestMacro2.xlsm!TheMacro"
ObjWB.Close
Set ObjWB= Nothing
Set ObjExcel = Nothing
Where the macro inside the Excel file TestMacro2.xlsm was pretty simple
Sub TheMacro()
MsgBox ("Hello")
End Sub
It worked without any problems.
Notice that instead of trying to close Excel application (ObjExcel.Application.Quit), I just close the opened Workbook (ObjWB.Close). This will avoid the script to close Excel while the user is working in another project.
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWB = ObjExcel.Workbooks.Open("C: \Net_Zero_Final.xlsm",0,True)
ObjExcel.Visible = True
ObjExcel.Run "Net_Zero"
ObjExcel.DisplayAlerts = False
ObjWB.Close = False
ObjExcel.quit
Set ObjWB = Nothing
Set ObjExcel = Nothing
It worked like this, I just passed ObjExcel.DisplayAlerts = False
it won't display any alerts and it will simply close the excel without saving it.
i hope this is right.

Save changes in an Excel spreadsheet that were made using MS Access VBA

In my database, I have a VBA script that opens an Excel spreadsheet and removes the first row so the new first row is the header row.
When I save the file in Access VBA and open the file in Excel, nothing is there.
If I reimport the spreadsheet, the data is visible.
I changed my script around and if I don't save the changes the file is ok. If I save the changes then this problem appears.
dim sheetpath as string
dim xl as excel.application
dim xlbook as excel.workbook
dim xlsheet as excel.worksheet
sheetpath = "c:\users\me\export.xlsx"
set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)
xl.visible = true
set xlsheet = xlbook.Worksheets(1)
If xlsheet.Range("a1").mergecells = true then
xlsheet.cells.unmerge
end if
if xlsheet.range("a1") = "Values" then
xlsheet.rows(1).delete
end if
xlbook.close savechanges:=true
xl.application.qit
set xl = nothing
set xlbook = nothing
set xlsheet = nothing
Try a reboot.
But before you do that, check in Task Manager / Process Explorer, if you have multiple Excel processes still running.
Instead of
set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)
do
Set xlbook = GetObject(sheetpath)
Set xl = xlbook.Application
What's this?
xl.application.qit
It should be
xl.Quit
And clear your references in the correct order, from bottom to top:
set xlsheet = nothing
set xlbook = nothing
set xl = nothing

Azure Remote App: Import Excel Range Into Access

I have the following code within a form startup procedure in an Access Front End accessing an SQL Back End. The code below, which simply opens an excel file, goes to the data tab and retrieves the value from a named range, works fine. However, when I migrate the front end to Azure Image and Publish in Remote Desktop, while the value is retrieved and updated to the database, the procedure hangs up. Using Alt-Ctl-End to open Task Manager, I find Excel still open, and need to End Task on Excel before my Access front end moves forward in the procedure. My hunch, Excel is waiting for a response on something, but there are no dialogs open to deal with. Any ideas?
Set xl = CreateObject("Excel.Application")
Set xlbook = xl.workbooks.Open(sRWFile)
Set xlsheet = xlbook.worksheets("Data")
xl.Visible = False
xlbook.Windows(1).Visible = False
With xlsheet
dValue = .Range(sRWRange)
End With
xlbook.Close , True
Set xlsheet = Nothing
Set xlbook = Nothing
Set xl = Nothing
Set xl = CreateObject("Excel.Application")
Set xlbook = xl.workbooks.Open(sRWFile)
Set xlsheet = xlbook.worksheets("Data")
xl.Visible = False
xlbook.Windows(1).Visible = False
With xlsheet
dValue = .Range(sRWRange)
End With
xlbook.Close , True
**xl.quit**
Set xlsheet = Nothing
Set xlbook = Nothing
Set xl = Nothing

Excel application not closing from Outlook VBA function

I'm writing a sort of homegrown ticketing system for myself in Outlook VBA, and I'm using Excel to store all the persistant data. I have a function written in Outlook to get some data from the .csv and return it. This is all working fine, but after I close the workbook, quit the application, and set the app to nothing I still have an Excel process running! Here is my code:
Private Function GetNewTicketNumber() As Integer
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
With xlApp
.Visible = False
.EnableEvents = False
.DisplayAlerts = False
End With
Dim FileStr As String
Dim NumberBook As Workbook
Dim TheRange As Range
FileStr = "C:\OMGITSAPATH.csv"
Set NumberBook = Workbooks.Open(FileStr)
Set TheRange = NumberBook.Worksheets(1).Range("A1")
GetNewTicketNumber = TheRange.Value
TheRange.Value = TheRange.Value + 1
NumberBook.Save
NumberBook.Close
xlApp.Quit
With xlApp
.Visible = True
.EnableEvents = True
.DisplayAlerts = True
End With
Set xlApp = Nothing
End Function
Is there something that I'm doing wrong here? My problem is similar to the one here, but I have disabled DisplayAlerts... What can I do to fix this problem?
Try fully qualifying your references to Excel, from xl_doesnt_quit
The problem presented here is exactly what you have. This line
Range("a1").Value = Range("a1").Value + 1
leave the xl instance open
The most common cause of the problem is a 'global' reference to the automated application. Unfortunately, under some circumstances it is possible to directly refer to an entity (property/method/object) of the automated object. This reference effectively is global to the calling application. Hence, the reference remains in place as long as the calling program is active. Consequently, the operating system will not end the automated application while the caller is active.
Re-cut code below (which also uses late binding - which rules out the unqualified possibility).
Pls change you path to suit.
code
Private Function GetNewTicketNumber() As Long
Dim xlApp As Object
Dim objWB As Object
Dim objWs As Object
Dim FileStr As String
FileStr = "C:\temp\test.xlsx"
Set xlApp = CreateObject("excel.application")
With xlApp
.EnableEvents = False
.DisplayAlerts = False
End With
Set objWB = xlApp.Workbooks.Open(FileStr)
Set objWs = objWB.Sheets(1)
GetNewTicketNumber = objWs.Range("A1")
objWs.Range("A1") = objWs.Range("A1") + 1
objWB.Save
objWB.Close
Set objWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Function

Resources