Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Go Back  Xtreme Visual Basic Talk > > > Closing Excel.EXE from vb.net app


Reply
 
Thread Tools Display Modes
  #1  
Old 06-09-2010, 01:37 PM
Mourvin Mourvin is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default Closing Excel.EXE from vb.net app


Hey guys,

First time poster...

I work for a financial firm doing vb.net work.
I open an excel doc and populate it using calls to range...
i however dont dim the range...I call xlsSheet.Range.Value
I have about 30 ranges i update in this method before printing

do i need to set the .Range.Value to Nothing in order to close the excel.exe

-Mourvin The Madman
Reply With Quote
  #2  
Old 06-09-2010, 01:43 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Hi Mourvin,

Welcome to the forum.

Quote:
Originally Posted by Mourvin
Do i need to set the .Range.Value to Nothing in order to close the excel.exe?
The short answer to your question is no, you do not. In fact, assigning the value of Nothing to a range would have the effect of clearing the contents (the formula &/or value) of the cell, but would not release any memory.

To release the memory of these range objects, you should call GC.Collect followed by GC.WaitForPendingFinalizers. For more on this, have a look at the Automating Office Programs with VB.NET.

Give it a read and then come back if you have any questions or problems...

Good luck!
Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #3  
Old 06-09-2010, 02:22 PM
Mourvin Mourvin is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default

Thank You

I actually read through that first, i follow your template exactly
I am using the double call to Collect and WaitForPendingFinalizers, and I release my sheet, book and app afterwords

before the first gc.collect
i call close on the book and quit for the app...

i just cant get the app to close
im on windows xp professional, visual studio 2005 and .net framework sp3

not that i think this will make a difference but in the section about suing the custom method with marshal in it, for the header u have (Of TComType)(ByRef rcw As ComType)

I could not seem to find ComType

-Thx
-Mourvin
Reply With Quote
  #4  
Old 06-10-2010, 06:51 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

My apologies, Mourvin,

That line should read:

Quote:
Shared Sub FinalReleaseAnyComObject(Of TComType)(ByRef rcw As TComType)
We'll get that fixed in the tutorial. Thanks for pointing that out.

Once you fix this, if you still have problems, show your code and we'll help you out...

- Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #5  
Old 06-10-2010, 07:43 AM
Mourvin Mourvin is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default

Hey Mike,

I think I fixed it. In my document I make changes to the sheets ranges using this notation

xlsSheet.Range("A1").Value = Data

when i commented out all changes to the doc, excel closed

I beleive if you are making changes, all references must have a created reference that can be destroyed

looks like I will be doing a lot of dim'ing, ill let you know how it turns out when im done testing this hypothesis, got a lot of changes to make

-Mourvin
Reply With Quote
  #6  
Old 06-10-2010, 08:38 AM
Mourvin Mourvin is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default

Hey Mike,

Yeah I'm not comfortable with posting my code since its a company's system

however i can tell you now that the excel.exe closes so long as Im not updating

even the process of assigning the ranges
<CODE>
With xlsSheet
ra1 = .Range("A1")
'etc etc
End With
</CODE>

Even this is causing the excel process to remain open
note: i dim and release using Marshal.FinalReleaseComObject

I send all obj's to a Public Shared Sub tthat takes an object, calls the release and sets it to Nothing

I am so lost

-Mourvin
Reply With Quote
  #7  
Old 06-10-2010, 08:46 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Mourvin,

Your approach to correcting this will work, but it is extremely easy to make a mistake. If you ever forget to declare a reference and then explicitly release it, then Excel will hang. Note that even a simple for-each loop, iterating through the cells of a range would create unreferenced range objects.

You can do it this way, but you must be extraordinarily careful -- and if you ever make a mistake there is no way to track down where the error is, except to look for it by eye. This can be ok for a very small project, but is an impossible task for a medium or large project.

Instead, I would suggest that you use GC.Collect and GC.WaitForPendingFinalizers to clear out your unreferenced objects. For example, you could use the following code to automate Excel, set the value of a cell, and then exit without having Excel hang:

Code:
Sub AutomateExcel()
    Dim excelApp As New Excel.Application With {.Visible = True}
    Dim workbook As Excel.Workbook = excelApp.Workbooks.Add()
    Dim worksheet As Excel.Worksheet = workbook.Worksheets.Add()
    ' -------------------------------------------------------------------

    worksheet.Range("A1").Value = "Hello world!"

    MessageBox.Show ("Are you ready to clean up?")

    ' ------------------------------------------------------------------
    ' Cleanup

    GC.Collect()
    GC.WaitForPendingFinalizers()

    Marshal.FinalReleaseComObject(worksheet)

    workbook.Close(SaveChanges:=False)
    Marshal.FinalReleaseComObject(workbook)

    excelApp.Quit()
    Marshal.FinalReleaseComObject(excelApp)
End Sub
Notice that in the above that the named references 'worksheet', 'workbook' and 'excelApp' are all explicitly released, but the range object for cell "A1" is released via the calls to 'GC.Collect' and 'GC.WaitForPendingFinalizers'.

You always have to explicitly release your named references, but when using constructs like a for-each loop it will be very inconvenient and error-prone to do so. So it is very important to have calls to GC.Collect() and GC.WaitForPendingFinalizers() that will clear out any variables to which you do not have a named reference.

Does this make sense? If you are still having trouble getting your code to release when using 'GC.Collect' and 'GC.WaitForPendingFinalizers', show your code and I'm sure that we can help you out.

- Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #8  
Old 06-10-2010, 08:49 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Quote:
Originally Posted by Mourvin View Post
even the process of assigning the ranges
<CODE>
With xlsSheet
ra1 = .Range("A1")
'etc etc
End With
</CODE>

Even this is causing the excel process to remain open
note: i dim and release using Marshal.FinalReleaseComObject
You are dimming and releasing your 'ra1' variable? Can you show this code? How about for your 'xlsSheet'?

I know that you can't show your private code, that's fine, but you need to show the code for all the variable declarations and then all the cleanup code at the end for us to be able to help you. You do not have to show any of the other code in the middle however.

- Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #9  
Old 06-10-2010, 10:02 AM
Mourvin Mourvin is offline
Newcomer
 
Join Date: Jun 2010
Posts: 5
Default

Hey Mike no worries, I've been working on this for days

I decided to use a msg service to send the app.HWND to the kernel to be killed

doesnt kill all the excel.exe processes as this is a server app with asynchronous msging

works well, boss likes...

Thanks for your help though!

-Mourvin
Reply With Quote
  #10  
Old 06-10-2010, 10:15 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Killng the process has a bad reputation, but I find that it works just fine as long as you've called Excel.Application.Quit first. So I would have no problem using it for automation (although it would be a bad idea if your code were running as an add-in.)

Glad it works for you!
,
Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
 
Closing Excel.EXE from vb.net app
Closing Excel.EXE from vb.net app
 
-->