UFT: Excel Process Keeps Running and freezes execution - excel

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()

Related

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.

VB.Net - Background Excel process REOPENS after closing file

I have been searching all over for this problem, but I can't seem to find one that quite matches my issue.
I am creating a new excel file, filling it with data, and showing it to user. While monitoring the task manager i can see that once the file has been created and open for the user, the background process disappears as it is supposed to. (The main process is still running up top because the file is open.)
The problem I have is that once the user closes the file, Excel background process pops back up in list and won't go away until program (that generated the file) is closed.
This is the clean up that I am using;
Dim xlObject As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet = Nothing
xlBook = xlObject.Workbooks.Add
xlSheet = xlBook.Worksheets(1)
'Fill data and do some formatting
xlBook.SaveAs("FileName")
xlObject.Visible = True
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
xlSheet = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
xlBook = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlObject)
xlObject = Nothing
Is there something that I am missing? Why is the background process continuing and only go away once the creating program is closed?
This is using Visual Studios 2013,
Office 365 Excel,
and Windows 10 Pro
You see, in Interop, you should avoid accessing an object through another object such as
xlBook = xlObject.Workbooks.Add
This is called the two dot rule. The reasoning is that the Workbooks object is then referenced in .NET and orphaned (and should also be killed as you have the other three)
So make a reference to the Workbooks, then kill that as well.
Dim xlApp As New Excel.Application()
Dim xlBooks = xlApp.Workbooks
Dim xlBook = xlBooks.Add
Dim xlSheet = DirectCast(xlBook.Worksheets(1), Excel.Worksheet)
Try
xlBook.SaveAs("FileName.xlsx")
xlApp.Visible = True
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
xlSheet = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
xlBook = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks)
xlBooks = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
End Try
And, I always struggle with this, because according to the preeminent expert in vb.net on StackOverflow, HansPassant, the two dot rule is just superstition, as he said "Feel free to continue to ignore [the two dot rule], it is nonsense and causes nothing but grief" but in the case of your question, I can only make it work by following the two dot rule, and NOT by using Hans' solution:
GC.Collect()
GC.WaitForPendingFinalizers()
Perhaps Hans will see this and set us straight!

Access VBA: working with an existing excel workbook (Run-Time error 9, if file is already open)

I'm writing a macro in Access that (hopefully) will:
create an Excel worksheet
set up and format it based on information in the Access database
after user input, will feed entered data into an existing Excel master file
Opening the blank sheet etc. is working absolutely fine, but I'm stuck trying to set the existing master file up as a variable:
Sub XLData_EnterSurvey()
Dim appXL As Excel.Application
Dim wbXLnew, wbXLcore As Excel.Workbook
Dim wsXL As Excel.Worksheet
Dim wbXLname As String
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
wbXLname = "G:\[*full reference to file*].xlsm"
IsWBOpen = fnIsWBOpen(wbXLname)
'separate function (Boolean), using 'attempt to open file and lock it' method
'from Microsoft site.
If IsWBOpen = False Then
Set wbXLcore = appXL.Workbooks.Open(wbXLname, True, False)
'open file and set as variable.
ElseIf IsWBOpen = True Then
wbXLcore = appXL.Workbooks("ResultsOverall.xlsm") 'ERROR HERE.
'file is already open, so just set as variable.
End If
Debug.Print wbXLcore.Name
Debug.Print IsWBOpen
Set appXL = Nothing
End Sub
When the file is closed, this works perfectly. However, when it's open I get:
Run-Time error '9':
Subscript out of range
I'm only just starting to teach myself VBA (very trial and error!) and nothing else I've seen in answers here / Google quite seems to fit the problem, so I'm a bit lost...
Considering that it works fine when the file is closed, I suspect I've just made some silly error in referring to the file - perhaps something to do with the 'createobject' bit and different excel instances??
Any suggestions would be much appreciated! Thanks
Thank you #StevenWalker
Here's the working code:
Sub XLData_EnterSurvey()
Dim appXL As Excel.Application
Dim wbXLnew As Excel.Workbook, wbXLcore As Excel.Workbook
Dim wsXL As Excel.Worksheet
On Error GoTo Handler
Set appXL = GetObject(, "Excel.Application")
appXL.Visible = True
Dim wbXLname As String
wbXLname = "G:\ [...] .xlsm"
IsWBOpen = fnIsWBOpen(wbXLname)
If IsWBOpen = False Then
Set wbXLcore = appXL.Workbooks.Open(wbXLname, True, False)
ElseIf IsWBOpen = True Then
Set wbXLcore = appXL.Workbooks("ResultsOverall.xlsm")
End If
Set appXL = Nothing
'-------------------Error handling------------------
Exit Sub
' For if excel is not yet open.
Handler:
Set appXL = CreateObject("Excel.Application")
Err.Clear
Resume Next
End Sub
Sorry I'm on my phone so I can't go in to too much detail or do much with the code but at a glance I think you might need to add an error handler so that if the file is already open, a different line of code is executed.
Add 'On error go to handler' (before creating the excel object) and at the bottom
Of your code add 'handler:'. In the error handler, use get object rather than create object.
You will have to ensure you use exit sub before the error handler or it will run the handler every time you run the code.
You can see an example of what I mean here: How to insert chart or graph into body of Outlook mail
Although please note in this example it's the other way round (if error 'getting' outlook, then create it).
Example in link:
Set myOutlook = GetObject(, "Outlook.Application")
Set myMessage = myOutlook.CreateItem(olMailItem)
rest of code here
Exit Sub
'If Outlook is not open, open it
Handler:
Set myOutlook = CreateObject("Outlook.Application")
Err.Clear
Resume Next
End sub
If you move the appXL.Workbooks statement to the debugging window, you will find that the names of the items in that collection are without extension.
So in your case, I'm guessing the line should read:
wbXLcore = appXL.Workbooks("ResultsOverall")

Remove Excel task from Task manager after running from Access using VBA

I will now take the opportunity to ask here, I have really tried a lot of different way, but it seems that I am not able to be able to close the Excel task in task-manger, It hangs until I close Access completely, annoying, because I can not run two different jobs using Excel from Access. Second job will give me errors.
I have made some comments to where I still is able to get rid of Excel.
The purpose for the code is to run some query's and export data to excel and then lock the excel sheet so users only can fill in answers to the data.
Code:
Private Sub Command65_Click()
Dim r As Double
'On Error GoTo Error_Handler
Dim objExcel As Excel.Application
Dim objWorkbook As Workbook
Dim objWorksheet As Worksheet
Dim dbs As DAO.Database
Dim rSt As DAO.Recordset
Set dbs = CurrentDb
Set rSt = CurrentDb.OpenRecordset("qry_VC_Confirmation")
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
'objExcel.Quit ' at this point it still works to close again
'Set objExcel = Nothing ' at this point it will remove from task manager
Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)
'Set objWorkbook = Nothing ' can close still at this stage
'Set objWorksheet = Nothing ' can close still at this stage
'objExcel.Quit ' at this point it still works to close again ?
'Set objExcel = Nothing ' at this point it still will not remove from task manager
iFld = 0
irow = 1
For icol = 1 To (rSt.Fields.count)
objWorksheet.Cells(irow, icol) = rSt.Fields(iFld).Name
objWorksheet.Cells(irow, icol).Interior.ColorIndex = 1
objWorksheet.Cells(irow, icol).Font.ColorIndex = 2
objWorksheet.Cells(irow, icol).Font.Bold = True
iFld = iFld + 1
Next
'Set objWorkbook = Nothing '
'Set objWorksheet = Nothing '
'objExcel.Quit ' at this point it still works to close Excel again ?
'Set objExcel = Nothing ' at this point it will still remove from task manager
irow = 2
If Not rSt.BOF Then rSt.MoveFirst
Do Until rSt.EOF
iFld = 0
lRecords = lRecords + 1
For icol = 1 To (rSt.Fields.count)
objWorksheet.Cells(irow, icol) = rSt.Fields(iFld)
iFld = iFld + 1
Next
irow = irow + 1
rSt.MoveNext
Loop
r = irow - 1
Columns("A:F").EntireColumn.AutoFit
ActiveSheet.Protection.AllowEditRanges.Add Title:="Unprotected", Range:=Range("F2:F" & r)
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="secret"
objWorkbook.SaveAs ("C:\Dropbox\VC_Confirmation.xlsx")
ExitSub:
Set objWorkbook = Nothing '
Set objWorksheet = Nothing '
objExcel.Quit ' at this point it still works to close excel again ?
Set objExcel = Nothing ' at this point it will **NOT** remove from task manager
Exit Sub
Error_Handler:
MsgBox Error$
Resume ExitSub
End Sub
In the comments you mentioned that you had reset your code (i.e. pressed the stop button). This means that the portion of your code that kills excel did not run, thus leaving an open session of excel. There is a small (possibly semantic) issue with your code, but I don't believe that's what was causing your issue. Regardless, you should properly shut down the application like this.
ExitSub:
If Not objWorksheet Is Nothing Then
set objWorksheet = Nothing
End If
' You have to check for the workbook's existence before
' you try to close something that isn't there. This avoids runtime errors.
' Since your error handler points you back here, this code always runs, so
' The workbook might not be open.
If Not objWorkbook Is Nothing Then
objWorkbook.close
Set objWorkbook = Nothing
End If
' Same goes for quitting the application
If Not objExcel Is Nothing Then
objExcel.Quit
Set objExcel = Nothing
End If
Exit Sub
Error_Handler:
' error handling code here
Resume ExitSub
End Sub
Columns("A:F").EntireColumn.AutoFit
Adding as an answer just in case. Fully qualify this with the worksheet name and try again. This same problem was a huge bother to me too. You have to qualify 100% of your references, no matter what. Also, be super careful about using With statements on ranges, worksheets etc. So change it to ObjWorksheet.Columns("A:F")... instead

I am getting run time error 462

HI,
I have the following piece of code in Access.
Dim objSht As excel.Worksheet
Dim objexcel As New excel.Application
Dim wbexcel As excel.Workbook
Dim wbExists As Boolean
Dim objRange As excel.Range
Dim isFileAlreadyPresent As Boolean
Set objexcel = CreateObject("excel.Application")
Set wbexcel = objexcel.Workbooks.Open(file_name)
Set objSht = wbexcel.Worksheets(table_name)
isFileAlreadyPresent = True
objSht.Activate
objSht.Range(Range_para).Select
Charts.Add
ActiveChart.chartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets(table_name).Range(Range_para), _
PlotBy:= xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.HasLegend = False
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.text = CHart_title
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
If isFileAlreadyPresent = True Then
wbexcel.Save
Else
wbexcel.SaveAs (file_name)
End If
objexcel.Visible = True
wbexcel.Close
I am having two problems. Every second time I run the code I get an run time error 462 (The remote server machine does not exist or is unavailable ) at line Charts.add.
I know that I am not using the objexcel property correctly but I am not sure where I am going wrong.
Also after the code is run, even though excel closes. The process runs in the background and this interferes with the next run of the code. How do I close excel and get rid of it from task manager processes also?
I think you will need to create the chart object like this, since your using late binding it won't know what "Charts" is unless you call it from the parent object.
objexcel.Charts.Add
Error 462 usually means something isn't qualified right, even though the message is sort of cryptic.
Just as a quick fix trying setting everything you declare/use to nothing at the end of your code to ensure that nothing is left open or active.
Set objSht = Nothing
Let me know if that fixes the problem
As you your question
How do I close excel and get rid of it
from task manager processes also?
You should be able to use the Application.Quit command at the end of your code as long as you aren't running the sub from another application other than Excel. Or, you should be able to do an objexcel.Quit command. Another alternative method is to delegate this to a shell command: Shell "taskkill /f /im excel.exe".
I hope this helps. Did you get a working piece of code yet?

Resources