Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2003

Tip: Looking for answers? Try searching our database.

Killing Excel.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Marsden - 15 Sep 2003 15:10 GMT
Hello,

I has a routine which is writen in VB.Net to open an excel workbook, update
it, save it and close it.

I am performing all the necessary steps to destroy a reference to excel,
including "ReleaseComObject" etc.
but I still have a EXCEL.EXE process running in task manager which I can't
get rid of.

Here is a sample of the Code:

       Dim objXL As Excel.Application
       Dim objWB As Workbook
       Dim objProperty As Object

       Try

           objXL = New Excel.Application

           File.SetAttributes(strFile, FileAttributes.Normal)
           objWB = objXL.Workbooks.Open(strFile)

           For Each objProperty In objWB.CustomDocumentProperties
           If objProperty.Name = "xxx" Then
               blnFound = True
               Exit For
           End If

System.Runtime.InteropServices.Marshal.ReleaseComObject(objProperty)
           objProperty = Nothing
           Next

           If blnFound Then
               objProperty.Value = "abc"

System.Runtime.InteropServices.Marshal.ReleaseComObject(objProperty)
               objProperty = Nothing
           Else
               objWB.CustomDocumentProperties.Add("xxx", False,
Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, strValue)
           End If

           objWB.Close(True)
           File.SetAttributes(strFile, FileAttributes.ReadOnly)

           System.Runtime.InteropServices.Marshal.ReleaseComObject(objWB)
           objWB = Nothing

       Catch ex As Exception
           Throw ex
       Finally
           If Not objProperty Is Nothing Then

System.Runtime.InteropServices.Marshal.ReleaseComObject(objProperty)
               objProperty = Nothing
           End If
           If Not objWB Is Nothing Then

System.Runtime.InteropServices.Marshal.ReleaseComObject(objWB)
               objWB = Nothing
           End If
           If Not objXL Is Nothing Then
               objXL.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(objXL)
               objXL = Nothing
           End If
           GC.Collect()
End Try

any suggestion on the correct way to kill Excel.

regards Tim
Robert Jacobson - 16 Sep 2003 00:04 GMT
Excel will only quit if you call Quit on the Excel application object.
Destroying the reference doesn't kill the application.  This block of code
isn't calling quit, because you've earlier set objXL to nothing:

  objXL = nothing
   .....
  If Not objXL Is Nothing Then
   objXL.Quit()
   System.Runtime.InteropServices.Marshal.ReleaseComObject(objXL)
   objXL = Nothing
  End If

You just need to change the order of execution to the following:

1.  Call objXL.Quit()

2.  Call ReleaseComObject on objXL, objWB and objProperty.  (Optional, I
think, but a good practice for cleaning up the COM references.)

3.  Set objXL, objWB and objProperty to nothing.  (This is totally optional,
since these objects will automatically be destroyed when the procedure exits
and the variables go out of scope.)

Take a look at how it's done in this KB article (the Excel code).
http://support.microsoft.com/?kbid=306682

Hope this helps,
Robert Jacobson

> Hello,
>
[quoted text clipped - 70 lines]
>
> regards Tim
Tim Marsden - 16 Sep 2003 09:22 GMT
Robert, thanks for the reply.

I understand what you are saying, but I am not setting the objXL = nothing
before I use objXL.Quit.

I will check out the article mentioned.

Tim

> Excel will only quit if you call Quit on the Excel application object.
> Destroying the reference doesn't kill the application.  This block of code
[quoted text clipped - 71 lines]
> >             objWB.Close(True)
> >             File.SetAttributes(strFile, FileAttributes.ReadOnly)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objWB)
> >             objWB = Nothing
> >
[quoted text clipped - 23 lines]
> >
> > regards Tim
Robert Jacobson - 17 Sep 2003 00:14 GMT
You're very right -- I misread your code.  Sorry about that.

Just to be 100% sure that it's actually getting called, try setting the
breakpoint on the objXL.Quit line and make sure it gets called.  Otherwise,
hopefully the article will help.

> Robert, thanks for the reply.
>
[quoted text clipped - 112 lines]
> > >
> > > regards Tim
Leo - 17 Sep 2003 19:59 GMT
Tim,

The Garbage collection does the trick for me:
GC.collect()

Let me know if it works for you.

Leo.

> Robert, thanks for the reply.
>
[quoted text clipped - 112 lines]
> > >
> > > regards Tim
Parker Zhang [MSFT] - 17 Sep 2003 02:48 GMT
Hi Tim,

Your code seems ok, and simliar code also works on my side.

Please make sure there is no EXCEL in the process list before you start
your application.

If you try the application on another machine, does the problem occur?

If possible, please send me the whole project along with the excel file,
and I will check it for you.

--
Parker Zhang
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.