Solved

Excel Process Not Closing after Using "Microsoft.Office.Interop"

Posted on 2013-11-13
3
960 Views
Last Modified: 2013-11-13
Hi,

I am using the following code to open an existing Excel file and saving it as different format.

However the Excel.exe process is not closing...

  

Imports Microsoft.Office.Interop
      
Dim strPath As String
        Dim strFile As String
        Dim strExtension As String
        Dim strPathFile As String

        strPath = "c:\test\"
        strFile = "test"
        strExtension = ".xls"
        strPathFile = strPath + strFile + strExtension



        Try

            Dim xlApp = New Excel.Application
            xlApp.Application.DisplayAlerts = False
            Dim xlWorkBooks As Excel.Workbooks = xlApp.Workbooks
            Dim xlWorkBook As Excel.Workbook = xlWorkBooks.Open(strPathFile, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value)
            Dim xlSheets As Excel.Sheets = xlWorkBook.Sheets
            Dim xlWorkSheet As Excel.Worksheet = xlSheets(1)

            strFile = "test2"
            strPathFile = strPath + strFile + strExtension

            xlWorkSheet.SaveAs(strPathFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, False, False, _
                              Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, _
                              Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing)

            MsgBox("open")
            xlWorkBook.Close()
            xlWorkBooks.Close()
            xlApp.Quit()

            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBooks)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

        Catch ex As Exception
            MsgBox("error")
        End Try

Open in new window

0
Comment
Question by:spen_lang
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 500 total points
ID: 39644924
You need to carefully clean up all COM-objects that you used.

There is a function System.Runtime.InteropServices.Marshal.ReleaseComObject created for that.

See the following article for more information: http://support.microsoft.com/kb/317109/en-us
0
 

Author Comment

by:spen_lang
ID: 39645173
That is great thanks. Do you know how I would specify what sheet to open?
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 39645228
I think you can use something like

Dim sheet = xlWorkBook.Sheets("mysheetname")

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now