Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Outlook and VBA (http://www.outlookbanter.com/outlook-vba/)
-   -   Access Excel (whether already open or not) from Outlook (http://www.outlookbanter.com/outlook-vba/84088-access-excel-whether-already-open.html)

wpiet January 6th 09 07:23 PM

Access Excel (whether already open or not) from Outlook
 
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.
--
Will

Norman Yuan January 6th 09 08:18 PM

Access Excel (whether already open or not) from Outlook
 
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)

"wpiet" wrote in message
...
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.
--
Will



wpiet January 6th 09 08:30 PM

Access Excel (whether already open or not) from Outlook
 
That would be why.
I'll file that one under "You da man, Norman."
Thanks
--
Will


"Norman Yuan" wrote:

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)

"wpiet" wrote in message
...
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.
--
Will





All times are GMT +1. The time now is 03:35 PM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com