?
Solved

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

Posted on 2013-11-13
3
Medium Priority
?
1,029 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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

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