Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi

 

I have a .net project written in vb.net and i am having a problem killing ALL instances of the excel process in the task manager. I had this problem with a windows service written in vb.net but I have managed to fix that problem but the same code doesn't fix the problem I am having in my website

 

The Code:

 

Dim exObj As New Excel.Application()

Dim exWorksheet As Excel.Worksheet

Dim exWorkbook As Excel.Workbook

 

exWorkbook = exObj.Workbooks.Open(("Template.xlt")

exWorksheet = exWorkbook.Worksheets(1)

 

'Code in here which writes to the excel file and saves it for furture use

 

 

'attempts to close the excel process

 

exWorkbook.Close()

exObj.Workbooks.Close()

exObj.Application.Quit()

exObj.Quit()

 

exWorksheet = Nothing

exWorkbook = Nothing

exObj = Nothing

 

GC.Collect()

GC.WaitForPendingFinalizers()

 

 

if anyone can help it would be much appreciated

Edited by tylee
Posted

Here is my working code which opens the file, changes it and saves it. I finally got it to destroy the process with the help of the previous post.

 


Dim MyExcel As New Excel.Application()
       Dim oWorkbooks As Excel.Workbooks = MyExcel.Workbooks
       Dim theWorkbook As Excel.Workbook = oWorkbooks.Add
       Dim oSheet As Excel.Worksheet

       theWorkbook = oWorkbooks.Open("C:\TEST.xls")

       oSheet = MyExcel.ActiveSheet()

'ALTER THE WORKSHEET HERE
      


       theWorkbook.Save()

       theWorkbook.Close()
     
       MyExcel.Quit()

       oSheet = Nothing
       theWorkbook = Nothing
       oWorkbooks = Nothing
       MyExcel = Nothing

       GC.Collect()
       GC.WaitForPendingFinalizers()

  • 1 month later...
Posted
I've been having the same problem with Excel hanging processes. I copied and pasted the exact code below and it still hangs the process. I've also tried working with the "Process" object, but I get an Access denied error. Any ideas? This is holding up my project big time....
Posted

Following should work:

 

MyExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(MyExcel);
MyExcel = null;

 

But you have to do this with all your little objects (like Excel.Range, Excel.Cell etc.) When you clean all this objects, your Excel process should end. Woked for me fine!!!

 

Regards,

Stefan

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...