I need VBA Assistance to test for instances

B

Brent E

Good afternoon,

I am using an Access module and using object variables to control Excel and
Word, I need to know what a VBA command is to test to see if Excel is open,
and if not to open the program, and do same for Word. Any suggestions? Thanks.

Cordially,
 
K

Ken Snell [MVP]

Generic:

Dim xlsApp As Object
On Error Resume Next
Set xlsApp = GetObject(,"Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set xlsApp = CreateObject("Excel.Application")
End If
 
D

Douglas J. Steele

One way is to use GetObject and trap for the error that will occur if the
application isn't already running:

On Error Resume Next

Dim objExcel As Object ' Excel.Application

Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcel = CreateObject("Excel.Application")
End If

I'm using Late Binding above, which means you don't even have to have a
reference set to Excel. If you've got the reference, replace Object with
Excel.Application in the declaration.

Another way is to use the FindWindow API function. There's a complete
example in http://www.mvps.org/access/api/api0007.htm at "The Access Web"
 
Top