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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Check to See If An Excel Application is Running From VB

Status
Not open for further replies.

frankR40

Programmer
Feb 3, 2004
3
US
I have a VB program which imports data from an excel sheet. At differnt times and on different forms in the program, different excel sheets are being opened and closed. I new a way to check if an Excel Application is running.

ex: If(Application."Running/Open/something"=True)Then

I know that isn't correct but that is what I need to happen.

Thanks for any suggestions.
 
You can try a couple of methods. One involves using API calls to iterate through the open applications looking for one that is Excel. The easier way would be to try to return an open instance of it. e.g.

Set MyXL = GetObject("Excel.Application")

If it doesn't work then Excel is not open, if it does work MyXL will be referencing it.
 
I am sure that there should be a way to do this through the Excel Object Library as you seem to be suggesting in your question, but I haven't used that too much so I can't help you there. Here is code that will check via task manager to see if excel is running though I am not too sure if this will help if you only started Excel through your vb program. I am sure someone will chime in with the answer through the Object Library though.

This is an adaptation from LPlates' post at Thread222-738582

Code:
Option Explicit

Private Const WM_CLOSE = &H10
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
                                   (ByVal hwnd As Long, ByVal wmsg As Long, _
                                    ByVal wparam As Long, lparam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 
Private Sub Command1_Click()
Dim Hndl As Long

Hndl = FindWindow(vbNullString, "Microsoft Excel - Book1")
    If Hndl <> 0 Then
        MsgBox (&quot;Excel is running&quot;)
    End If
End Sub

Hope that helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top