excel.exe doesn't quit (vb.net) [duplicate] - vb.net

This question already has answers here:
How do I properly clean up Excel interop objects?
(43 answers)
Closed 8 years ago.
Dim oXL As Object
Dim oWB As Object
Dim oSheet As Object
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
.........
oXL.Quit()
oWB = Nothing
oXL = Nothing
oSheet = Nothing
I can see the application opened in task manager ... Why ?

You need to do what #Afnan Makhdoom said, but not only what he said you also need to call...
GC.Collect()
This cleans up the objects being used and will remove it from task manager. Just releasing the objects won't remove it from task manager, you need to force the garbage collector.

Try this so it will remove the task manager process after you open the excel file:
Dim proc As System.Diagnostics.Process
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
proc.Kill()
Next

What you need to do is release the objects first, it will surely exit the Excel.exe
oSheet.Close(False)
oXL.Quit()
releaseObject(oXL)
releaseObject(oWB)
releaseObject(oSheet)
oWB = Nothing
oXL = Nothing
oSheet = Nothing

Related

Powerpoint VBA dont close Excel

My VBA code in Powerpoint wont "kill" my excel application.
it still runs if i have a look in task manager.
I would like to close it the right way rather than kill.
Anyone that could help me? I already searched and tried to get it working but with no luck.
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If Wn.View.CurrentShowPosition = 2 Then
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWorkBook = xlApp.Workbooks.Open("D:\ELE_powerpoint\book1.xlsx", True, False)
With xlWorkBook.ActiveSheet
txt1 = xlWorkBook.sheets(1).Range("A2")
txt2 = xlWorkBook.sheets(1).Range("A3")
txt3 = xlWorkBook.sheets(1).Range("A4")
End With
ActivePresentation.Slides(2).Shapes("a2").TextFrame.TextRange = txt1
ActivePresentation.Slides(2).Shapes("a3").TextFrame.TextRange = txt2
ActivePresentation.Slides(2).Shapes("a4").TextFrame.TextRange = txt3
Set xlApp = Nothing
Set xlWorkBook = Nothing
End If
End Sub
Try xlApp.Quit before Set xlApp = Nothing
Set xlApp = Nothing
That instruction sets the xlApp object reference to Nothing. It doesn't destroy the object it's referring to, but it prevents further code from using that reference.
Excel is a rather complex application; creating an instance of an Excel.Application object has many implications, and in order to properly shut itself down, it needs to run through a certain sequence of instructions (unload Excel and COM add-ins, close any opened file, tear down the VBE if it was initialized - which in turn may need to tear down its own add-ins, etc.) - by setting your reference to Nothing, you say "I don't need it anymore" - but what of the COM add-ins that still use it? That workbook that's still open is a Workbook object with an Application property that also references the very same object you were referring to - and these references aren't gone yet!
As Tim and Tom said, you need to call the Quit method of the Excel.Application instance, so that it can clean itself up.
And if that's the last thing your code does, then you probably don't even need to Set xlApp = Nothing, since the VBA runtime will know that it doesn't need to hold on to the xlApp reference after it's out of scope; Set xlWorkbook = Nothing is superfluous as well.
Quitting Excel will close the unmodified workbook you've opened, but since you opened it, I'd argue that it's good form to close it yourself - just call xlWorkbook.Close before you call xlApp.Quit.

Application not released from Task Manager [duplicate]

This question already has answers here:
Application not quitting after calling quit
(11 answers)
Closed 7 years ago.
I'm trying to write some data from my windowsform into a Excel file, this works.
' Excel load data
Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim oWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
oWorkBook = oExcelApp.Workbooks.Open("C:\Temp\Test.xlsx")
oWorkSheet = oWorkBook.Worksheets(1)
oWorkSheet.Range("A1").Value = "Test"
oWorkBook.Save()
oWorkBook.Close()
Problem is: When I am done Excel is still running in my task manager. When I press the button like 10 times there are 10 Excel references in my task manager.
Question: How can I fully unload Excel after writing the value into the Excel?
You need to Quit Microsoft Excel and then release the objects.
Code referenced from this answer: The proper way to dispose Excel com object using VB.NET? and Excel application not quitting after calling quit
'Excel load data
Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim oWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
oWorkBook = oExcelApp.Workbooks.Open("C:\Temp\Test.xlsx")
oWorkSheet = oWorkBook.Worksheets(1)
oWorkSheet.Range("A1").Value = "Test"
oWorkBook.Save()
oWorkBook.Close()
oExcelApp.Quit()
'Release object references.
releaseObject(oWorkSheet)
releaseObject(oWorkBook)
releaseObject(oExcelApp)
----------------------------------------------------------------------------
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
You have to close the connection you just opened. You can do this by adding this line after your current code:
oExcelApp.Quit();
Source & more information: here
More information about the quit() method here.
Important: if you have open workbooks that are not saved yet, this method will show a dialog box asking to save.
If you don't want this, you either have to (1): save all open workbooks or (2) setting DisplayAlerts to false.
(1)
workbooks.Save()
(2)
oExcelApp.DisplayAlerts = false

Object Library not registered

I am using Access(2007) vba to process an Excel(ACCDB) workbook on a Sharepoint 2007 document library. In the process, I update two properties using the ExcelWB.ContentTypeProperties() proerty. It's been working fine literally for years. Today, it stopped working. It throws a "Object Library not registered" error. Research seems to suggest I've somehow gotten an old DLL for Excel registered, and the interface is using an old DLL that doesn't support the property.
My libraries are all found, no changes were made (by me...) in the last month to the machine. It's a client machine, so I can't speak for MS updates, etc. that were made. How can I fix this? It's literally got my client sitting on her hands, unable to work.
Dim xlApp As Excel.Application
Dim xlWB As Excel.WorkBook
Dim xlSH As Excel.Worksheet
Dim xlRA As Excel.Range
Dim bValidWorkbook As Boolean
Dim lngTrueHighestRow As Long
Dim strWorkbookName As String
WorkbookMessage , 3 'Clears the message queue
bValidWorkbook = True
strNewTimesheetStatus = "Rejected"
Set xlApp = New Excel.Application
xlApp.visible = False 'Don't let the workbook be shown
Set xlWB = xlApp.Workbooks.Open(URL) 'Open the workbook
lngCurrentVendorID = ParseVendor(strCurrentVendorName) 'Sets up the vendor recordset and the Labor rate Recordset. If it returns 0, it couldn't find the vendor
If lngCurrentVendorID = 0 Then
bValidWorkbook = False
WorkbookMessage "Workbook Rejected -- Vendor could not be validated"
GoTo ExitWorkbook
End If
'
' If the workbook is read-only, we can't proceed
'
If xlWB.ReadOnly Then
bValidWorkbook = False
WorkbookMessage "Workbook Rejected -- Workbook is read-only state"
GoTo ExitWorkbook
End If
Set xlSH = xlWB.Worksheets(1) 'Must be the first worksheet in the workbook
strWorkbookName = xlWB.NAME
Set xlRA = xlSH.UsedRange 'xlRA is the range of cells in the first workbook that are "USED"
strSheetArray = xlRA 'This sets an array of variants to the two dimensional range xlRA
xlRA.EntireRow.Hidden = False 'Unhides all rows to prevent misunderstandings.
...
...
...
...
If Not xlWB.ReadOnly Then
xlWB.ContentTypeProperties("TimesheetStatus") = strValidateWorkbook***
xlWB.ContentTypeProperties("OverrideStatus") = strNewOverrideStatus
xlWB.Save
End If

Exception converting xls file to tab-delimited file

I have couple of ".xls" files on a folder and need to convert it into tab seperated values
Found a vb script for this ..please some body sugest how to do this?
I am getting couple of error when running this.I am not a vb programmer.Experts...please help
Public Sub Main()
Dim WScript As Object = Nothing '' with out nothing it was showing an error
Dim oExcel As Object
Dim oBook As Object
If WScript.Arguments.Count < 2 Then
WScript.Echo("Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv")
Wscript.Quit()
End If
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open(WScript.Arguments.Item(0)) ''item o might be excel
oBook = oExcel.Workbooks.Open("C:\Users\5A5.xls")
oBook.SaveAs(WScript.Arguments.Item(1), -4158)
oBook.Close(False)
oExcel.Quit()
WScript.Echo("Done")
End Sub
The exception:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation.
---> System.NullReferenceException: Object variable or With block variable not set.
Your problem is you are trying to use WScript but it has not been initialized (it's set to Nothing).
Try it without it:
Dim oExcel As Object
Dim oBook As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open("C:\Users\5A5.xls")
oBook.SaveAs("C:\Users\5A5.txt", -4158)
oBook.Close(False)
oExcel.Quit()

Reading excel files in vb.net leaves excel process hanging

The following code works fine but seems to leave instances of excel.exe running in the background. How do I go about closing out this sub properly?
Private Sub ReadExcel(ByVal childform As Fone_Builder_Delux.frmData, ByVal FileName As String)
' In progress
childform.sampleloaded = False
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(FileName)
xlWorkSheet = xlWorkBook.Worksheets(1)
Dim columnrange = xlWorkSheet.Columns
Dim therange = xlWorkSheet.UsedRange
childform.datagridHeaders.Columns.Add("", "") ' Super imporant to add a blank column, could improve this
For cCnt = 1 To therange.Columns.Count
Dim Obj = CType(therange.Cells(1, cCnt), Excel.Range)
childform.datagridSample.Columns.Add(Obj.Value, Obj.Value)
childform.datagridHeaders.Columns.Add(Obj.Value, Obj.Value)
Next
For rCnt = 2 To therange.Rows.Count
Dim rowArray(therange.Columns.Count) As String
For cCnt = 1 To therange.Columns.Count
Dim Obj = CType(therange.Cells(rCnt, cCnt), Excel.Range)
Dim celltext As String
celltext = Obj.Value.ToString
rowArray((cCnt - 1)) = celltext
'MsgBox(Obj.Value)
Next
childform.datagridSample.Rows.Add(rowArray)
Next
AdjustHeaders(childform)
childform.sampleloaded = True
End Sub
Short answer: close each item appropriately, then call FinalReleaseComObject on them.
GC.Collect()
GC.WaitForPendingFinalizers()
If xlWorkSheet Is Nothing Then Marshal.FinalReleaseComObject(xlWorkSheet)
If xlWorkBook Is Nothing Then
xlWorkBook.Close(false, false)
Marshal.FinalReleaseComObject(xlWorkBook)
End If
xlApp.Quit()
Marshal.FinalReleaseComObject(xlApp)
Long answer: read this answer to another question (the entire post is helpful too).
I ran into this problem and what I found worked was making sure I called the Close() method on all Workbook and Workbooks objects, as well as the Quit() method on the Excel Application object. I also call System.Runtime.InteropServices.Marshal.ReleaseComObject on every Excel object was instantiated. I do all this in reverse order of age, so the newest object gets cleaned up first and the oldest, which is the Application object, gets taken care of last. I don't know if the order really matters, but it seems like it might.
I've seen examples where GC.Collect() was called at the very end, but I've never had to do that to get the excel.exe process to end.