[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-11-13
3
Medium Priority
?
1,041 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Six Sigma Control Plans
Starting up a Project

650 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