Access Excel (whether already open or not) from Outlook

W

wpiet

What am I doing wrong with this code?

Dim XL As Object

On Error Resume Next
Set XL = GetObject("Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
XL.Visible = True

I was under the impression that, if an instance of Excel is already running,
GetObject would access it & 'Set' would assign it to the variable 'XL'.
However, when I step through it, 'XL' = Nothing & CreateObject opens
another instance of Excel.
 
N

Norman Yuan

D you know GetObject() takes TWO optional arguments? The first one is
PathName, and the second one is the ClassName.

Your code supplies "Excel.Application" as the first argument as PthName,
which points to nothing and omits the second argument, thus, the GetObject()
returns nothing.

The correct code should be:

Set XL=GetObject(, "Excel.Application)
 

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

Top