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!

Excel Application

Status
Not open for further replies.

PFairhurst

Programmer
Jul 30, 2002
18
GB
Does anyone know how to check if an Excel application is open? Such as rsSet.state does for a recordset?
Thank you
Pete
 
You can use the GetObject method to determine if an instance of Excel is open.
Code:
Private Sub Command1_Click()
    Dim xlApp As Object
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
        MsgBox &quot;Excel is NOT Open&quot;
        Err.Clear
    Else
        MsgBox &quot;Excel IS Open&quot;
    End If
    Set xlApp = Nothing
End Sub
 
Hi PFairhurst,

You need to add some code to dsi's post to deal with Excel 7(95) bugs. An Open Version of Excel 7 doesn't get placed into the Running Object Table (ROT) by default, thus GetObject will not detect it until you have registered it to the ROT yourself see the Microsoft Articles referenced Below:-

Private Declare Function SendMessage Lib &quot;user32&quot; Alias _
&quot;SendMessageA&quot; (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long


Public Function IsExcelInstance() As Boolean
'----------------------------------------------------------
' Author: Codefish
'
' Date: 17/05/2001
'
' History:
'
' Purpose: Checks if there is already an Instance of
' Microsoft Excel Running.
'
'----------------------------------------------------------
'Defer Error Trapping.
On Error GoTo Errhandler

Dim bExcelRunning As Boolean

'Call Function to Put Any Running Excel Application in
'the Running Object Table (ROT), Before you can use
'GetObject - Bug in Microsoft Excel
'Returns hWnd if Excel was Running, Hence then Use
'Excel Instance
If DetectExcel > 0 Then
'Test to see if there is a copy of Microsoft Excel
'already running.
'Check for Microsoft Excel by using the GetObject
'Function. Getobject function called without the
'first argument returns a Reference to an Instance
'of the Application. If the application isn't
'running, an error occurs.

'This Error can usually be Trapped and you can
'proceed.
Set xlApp = GetObject(, &quot;Excel.Application&quot;)

bExcelRunning = True

Else
'Set Flag - No Excel
bExcelRunning = False
End If

'Return If there is an Instance
IsExcelInstance = bExcelRunning

Exit Function

Errhandler:
'Error - Return False
IsExcelInstance = False

End Function

Private Function DetectExcel() As Long
'----------------------------------------------------------
' Author: Codefish
'
' Date: 4/02/2002
'
' History:
'
' Purpose: - Procedure Dectects a Running Excel and
' Registers it in the Running Object Table
' (ROT).
'
' Notes: This was introduced as a Fix to Numerous
' Automation Bugs Associated with Excel 7.0
' (95) Upwards.
'
' Earlier Versions of Excel Registered it in
' the ROT as Default
' but Later Versions Do Not. Code Below
' Fixes this.
'
' For more Info see Microsoft's Knowledge
' Base:
' Q138723 - Code to Access MS Excel Does Not
' Work in Version 7.0
' Q153025 - Microsoft Excel 95 Doesn't
' Respond Correctly to GetObject.
' Q134835 - Automation Error Using Excel
' Object in VB Procedure
' Q288902 - GetObject and CreateObject
' Behaviour of Office
' Automation Servers.
' Q292491 - Office Automation when Multiple
' Versions of Office Are Installed.
'----------------------------------------------------------

Dim hWnd As Long

'If Excel is Running this API Call Returns its Handle.
hWnd = FindWindow(&quot;XLMAIN&quot;, 0)

'Check if Excel is Running
If hWnd = 0 Then
'0 means Excel not running.
Exit Function
Else
'Excel is running so use the SendMessage API
'Function.
'Place it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If

'Return Handle if Excel is Open
DetectExcel = hWnd

End Function



Regards,

Codefish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top