Getting error on closing excel macro using vbscript - excel

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.

Related

UFT: Excel Process Keeps Running and freezes execution

I have an web automation framework in UFT where I've to update the customized excel report. During the report editing part of code, I am facing a strange issue related to excel process/vba object. I have this code stored in function library (.qfl) inside a function and it is called numerous times. The problem is, sometimes the UFT execution freezes and I see a running process of EXCEL.exe. Nothing happens on the screen for hours until I manually kill the process.
On error resume next
Dim objExcel, objWorkbook, objWorkSheet
Set objExcel = createobject("excel.application")
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open("SheetLocation")
Set objWorkSheet = objWorkbook.WorkSheets("SheetName")
intStepRow = objWorkSheet.Cells.Find("WC_01").Row
objWorkSheet.Cells(intStepRow, 7).Value = "SomeValue"
objWorkSheet.Cells(intStepRow, 8).Value = "SomeValue"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close True
objExcel.Quit()
Set objWorkSheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
On error goto 0
I tried putting a code at the end of function to kill the process but No luck. I also tried setting excel.Visible and .Display to true to see where exactly things go wrong but I don't see anything. I also tried removing the error handling but same, no luck.
Edit:
After enabling logging for each line, I found that below line is freezing the execution
Set objWorkbook = objExcel.Workbooks.Open("SheetLocation")
Thanks in advance for your time and suggestions :)
alternatively i find it interesting, could you try using the objExcel.quit() after you realese the object i.e. after you set wroksheet,workbook, excel = nothing.
e.g.
Set objWorkSheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
objExcel.Quit()

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

VBScript that runs a macro in excel throws an error

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

Error copying cells using PasteSpecial method

Set objExcel = createObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
iRow = objworkbook.sheets("Sheet1").Usedrange.rows.Count
objworkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
objExcel.visible = true
Set Obj1 = objexcel.workbooks.add()
objExcel.displayalerts = false
Obj1.saveas("Copied abcd")
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
I am trying to copy one file to another, but I get an error saying that the PasteSpecial method of the Range class failed.
The reason why PasteSpecial fails with the observed error is because the SaveAs operation clears the copied data. Move the SaveAs after the PasteSpecial and move the Copy right before the PasteSpecial. I would also recommend to put general configuration properties like Visible or DisplayAlerts right after the instantiation of the application object.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open _
("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
Set Obj1 = objExcel.Workbooks.Add
iRow = objWorkbook.Sheets("Sheet1").UsedRange.Rows.Count
objWorkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
Obj1.SaveAs "Copied abcd"

Selection.End(xlToRight) does not work

I am having a rough time getting this VBscript line to work with the excel object:
set fso=CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objExcel = createobject("Excel.application")
objexcel.Visible = true
objexcel.Application.ScreenUpdating = True
objexcel.Workbooks.Open dir & masterFileName
objexcel.Activeworkbook.Worksheets("xActive_User_Ratio").Activate
objexcel.Range("A1").Select
objexcel.Range(Selection, Selection.End(xlToRight)).Select
when I run this code I get an error:
Object required: 'Selection'
What am I doing wrong? Any example would be very much helpful.
Please Help
It's because you are running this from outside Excel.
Use objExcel.Selection instead of just Selection. So that your code knows that Selection is associated with the Excel Application. Additionally, you'll need to define xlToRight or replace it with it's numerical value.
Better yet, I'd use with and rewrite the whole thing like so:
Set fso = CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objexcel = CreateObject("Excel.application")
xlToRight = -4161 ' -4161 is the value of xlToRight
With objexcel
.Visible = True
.Application.ScreenUpdating = True
'using variables for workbook and worksheet to be explicit
Set wb = .Workbooks.Open(Dir & masterFileName)
Set ws = wb.Worksheets("xActive_User_Ratio")
ws.Activate
ws.Range("A1").Select
ws.Range(.Selection, .Selection.End(xlToRight)).Select
End With

Resources