Article ID: 155201
Article Last Modified on 10/11/2006
' Test to see if there is a copy of Microsoft Excel already running. On Error Resume Next ' Defer error trapping. ' GetObject function called without the first argument returns a ' reference to an instance of the application. If the application ' is not running, an error occurs. Note the comma used as the first ' argument placeholder. Set MyXL = GetObject(, "Excel.Application")
Option Explicit
Const WM_USER = 1024
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As Long) As Long
Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg _
As Long, ByVal wParam As Long, lParam As Long) As Long
Sub DetectExcel()
'=================================================================
'This procedure detects if Excel7 is running and registers it.
'=================================================================
'
Dim hwnd As Long
'Find the windows handle for Excel.
'This returns a valid handle number,
'or zero if Excel is not running
hwnd = FindWindow("XLMAIN", 0)
If hwnd = 0 Then
'Excel is not running
Exit Sub
End If
'Excel is running
'Registers Excel in the ROT
SendMessage hwnd, WM_USER + 18, 0, 0
End Sub
' Test to see if there is a copy of Microsoft Excel already running. On Error Resume Next ' Defer error trapping. ' GetObject function called without the first argument returns a ' reference to an instance of the application. If the application ' is not running, an error occurs. Note the comma used as the first ' argument placeholder. DetectExcel Set MyXL = GetObject(, "Excel.Application")
138723 Code to Access MS Excel Doesn't Work in Version 7.0
153029 Microsoft Excel 95 Doesn't Respond Correctly to GetObject
Additional query words: started open
Keywords: kbinterop kbprogramming KB155201