Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get instance of Running Excel Workbook

Status
Not open for further replies.

jkb17

Programmer
Nov 27, 2000
156
US
Hello,

I am writing a vb.net windows application. I need to test if an excel workbook is open and if so kill it. I can start the excel book with no problems from the application but just in case the book is open I would like to kill it so I can re-instantiate it.

Does anyone know how to find out if a particular excel book is open and if so kill it?
 
The application will not run on a user's machine so that's not a concern. What I need to do is grab an instance (preferably of JUST the excel workbook) so that I can close it and re-create it.
 
Is this what you are looking for?

Code:
        Dim procs As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For i As Integer = 0 To procs.Length - 1
            procs(i).Kill()
        Next
 
That would work fine. I would prefer to just kill the excel workbok (not the application) but if that's the only solution then it mya be suffice.
 
Oh I see now. Try this then

Code:
        Dim xl As Object = GetObject(, "Excel.Application")
        For i As Integer = 1 To xl.Workbooks.Count
            xl.Workbooks(i).Close(False)
        Next
 
Thank you, I'm going to give that a whirl now.
 
As a side note, I gave you a generic, late-bound solution. If you are using early binding and have access to the Excel objects before hand, you can obviously close the workbook with your pre-defined objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top