Application not released from Task Manager [duplicate] - vb.net

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

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.

.Net Interop Excel ExportAsFixedFormat() Exception HResult: 0x800A03EC

I'm currently implementing a method that generated multiple sheets and export them as PDF. For this I'm using the Microsoft.Office.Interop Library (v14.0.0.0) with .NET 4.5.2 . Running Office is 2016
My code:
Dim excel As New Application()
excel.Visible = False
excel.DisplayAlerts = False
Dim workbooks As Workbooks
workbooks = excel.Workbooks
Dim workbook As Workbook = workbooks.Add(Type.Missing)
[...]
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)
ReleaseComObject(workSheet)
workbook.Close()
ReleaseComObject(workbook)
excel.Quit()
ReleaseComObject(excel)
The ReleaseComObject() looks like this (according to Microsoft Support):
Private Sub ReleaseComObject(objectToRelease As Object)
While System.Runtime.InteropServices.Marshal.ReleaseComObject(objectToRelease) > 0
End While
objectToRelease = Nothing
End Sub
This works fine if I run this code for one iteration BUT I noticed that the EXCEL-Process is still running.
If I try to do this in batch-mode (in the meaning of a for-loop) I get an excetion when entering the 2nd interation:
System.Runtime.InteropServices.COMException (0x800A03EC): Ausnahme von HRESULT: 0x800A03EC
bei Microsoft.Office.Interop.Excel.WorkbookClass.ExportAsFixedFormat(XlFixedFormatType Type, Object Filename, Object Quality, Object IncludeDocProperties, Object IgnorePrintAreas, Object From, Object To, Object OpenAfterPublish, Object FixedFormatExtClassPtr)
bei Controller.CreateListing(DataTable data, Int32 year, String mandantShortName) in ...
Line that throws exception:
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)
For reseach/testing I debugged before reentering the loop and killed the excel-process but w/o any changes.
Anyone faced this problem as well? Solutions/Suggestions?
In order to address the issue with Excel not closing properly replace:
Private Sub ReleaseComObject(objectToRelease As Object)
While System.Runtime.InteropServices.Marshal.ReleaseComObject(objectToRelease) > 0
End While
objectToRelease = Nothing
End Sub
With this bit of code as illustrated by Siddharth Rout:
Private Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Loop While intRel > 0
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
You could use your While statement instead of the Do statement but I feel it reads better this way. The important bit here is GC.Collect().
You also have to make sure you release in the right order and release everything. This is usually in backwards order. So in your case, start off with the workSheet then the workbook then workbooks and then lastly excel:
ReleaseObject(workSheet)
workbook.Close()
ReleaseObject(workbook)
ReleaseObject(workbooks)
excel.Quit()
ReleaseObject(excel)
This is the code I put together to test:
Dim app As New Excel.Application()
app.Visible = False
app.DisplayAlerts = False
Dim wbs As Excel.Workbooks = app.Workbooks
Dim wb As Excel.Workbook = wbs.Add()
Dim ws As Excel.Worksheet = CType(wb.Sheets(1), Excel.Worksheet)
ReleaseObject(ws)
wb.Close()
ReleaseObject(wb)
ReleaseObject(wbs)
app.Quit()
ReleaseObject(app)
The process starts and once ReleaseObject(app) has been called the process then closes.
I just figured it out what the problem caused (own Bug).
But it still leaves the question open, why the Excel-Proccess ain't closing.
Try to run application or service as administration account. This solved my problem.

vb.net & office: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'

I'm developing a vb.net application and it should open and handle a excel file.
The problem is that in my pc (and the same could happen in other pc where it could be installed) I have Visio version 2013 and Office (excel, word, etc..) version 2010. Integrating excel in vb is so simple because more or less automatic, done in few steps.. but in the same time so stupid: calling interop.Excel functions, it addresses to the newest version of office intalled (2013), but I don't have excel for that version!
I searched a lot on the web about this issue, and every where I found the solution of deleting a registry key, well, it works fine, but every time I use Visio, it seems the registry key is recreated again.
My questions are:
can I select which version of excel I would like to link my application?
I can understand that fixing the version I should handle any office versions
manually.. but maybe I prefer doing this..
can I copy a dll in the application folder to be sure that the app uses that
version? I tried with Microsoft.Office.Interop.Excel.dll and Office.dll but still the issue
I don't want to follow the way of deleting the registry key programmatically, is not safe in my opinion..
I can't believe that the integration between office and .net is so stupid that is not able to use the right version of excel installed!!
;-P
Code:
Dim fileTest As String = FilePath
Dim APP As New Application
MsgBox(APP.Version)
Dim oSheet As Excel.Worksheet = Nothing
Dim workbook As Excel.Workbook = Nothing
Dim IndexFound As Integer = 0
Dim StartRow As Integer = 10
Dim DateSelected As String = ""
Dim resultOK As Boolean = False
Try
workbook = APP.Workbooks.Open(fileTest)
oSheet = workbook.Worksheets("Activities")
APP.DisplayAlerts = False
oSheet.Range("A2").Value = "test"
oSheet.Range("B3").Value = "Ciao"
Catch ex As Exception
MsgBox("Error: " + ex.Message)
If Not resultOK Then
If Not workbook Is Nothing Then
workbook.Close()
While System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook) > 0
End While
workbook = Nothing
End If
oSheet = Nothing
workbook = Nothing
APP.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(APP)
APP = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
End If
End Try
End Sub
Thanks
Andrea

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

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

word vba wont close excel application

How can I close excel aplication completely in word vba?
My code:
Dim ExcelApp As Object, ExcelBook As Object
Set ExcelApp = CreateObject("excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open(ActiveDocument.Path & "\data.xls")
'ExcelApp.Visible = True/False
ExcelApp.DisplayAlerts = False
'something with ExcelBook, just editing cells
ExcelBook.Close savechanges:=True
Excel.Application.Quit
Set ExcelBook = Nothing
Set ExcelApp = Nothing
When I open taskmanager there is still proces EXCEL running. Problem is, that I need to run macro multiple times and than my data.xls - excel file can be open read only.
You need to quit the instance you started. Replace
Excel.Application.Quit
with
ExcelApp.Quit
I find out why I had this problem. While running code that uses Automation to control Microsoft Excel, Visual Basic does not release reference until you end the program.
When I close word, excel process is closed as well.
More info here:
http://support.microsoft.com/kb/178510/en-us