I need VBA Assistance to test for instances

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,
 
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
 
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top