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 "user32" Alias _
"SendMessageA" (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(, "Excel.Application"
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("XLMAIN", 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