Accessing existing Excel instance

I

Ian Dunn

I'm simply trying to access an instance of Excel that has been opened
manually by the user in order to put a few values in the existing sheet.
Here's the code I've tried:

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = GetObject(, "Excel.Application")
oWB = oXL.ActiveWorkbook
oSheet = oWB.ActiveSheet

Unfortunately this doesn't work. What happens is that oXL.ActiveWorkbook
is "nothing" so the last line in this code fails when trying to access
it's ActiveSheet property.

The reason this happens seems to be that GetObject isn't getting the
existing Excel instance and is instead behaving exactly like
CreateObject and making a new instance with no workbooks. In fact
GetObject even works if Excel isn't currently running which I don't
think it's meant to. For example the following code (which is adapted
slightly from the help for GetObject):

oXL = GetObject(, "Excel.Application")
If Err().Number <> 0 Then
MsgBox("Excel was not running")
Else
MsgBox("Excel was running")
End If

This always reports that Excel was running even if it wasn't.

I'm using Visual Studio 2003 and have tried this with both Excel 2000,
and Excel 2003 with the same result.

Any idea what's going wrong or how I can do this properly?

Thanks
 
P

Paul Larson

Ian Dunn said:
I'm simply trying to access an instance of Excel that has been opened manually
by the user in order to put a few values in the existing sheet. Here's the
code I've tried:

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = GetObject(, "Excel.Application")
oWB = oXL.ActiveWorkbook
oSheet = oWB.ActiveSheet

Unfortunately this doesn't work. What happens is that oXL.ActiveWorkbook is
"nothing" so the last line in this code fails when trying to access it's
ActiveSheet property.

The reason this happens seems to be that GetObject isn't getting the existing
Excel instance and is instead behaving exactly like CreateObject and making a
new instance with no workbooks. In fact GetObject even works if Excel isn't
currently running which I don't think it's meant to. For example the following
code (which is adapted slightly from the help for GetObject):

oXL = GetObject(, "Excel.Application")
If Err().Number <> 0 Then
MsgBox("Excel was not running")
Else
MsgBox("Excel was running")
End If

This always reports that Excel was running even if it wasn't.

I'm using Visual Studio 2003 and have tried this with both Excel 2000, and
Excel 2003 with the same result.

Any idea what's going wrong or how I can do this properly?

After adding "Imports Excel=Microsoft.Office.Interop.Excel" and adding a
reference to the Excel 10 object model (which auto-includes
Microsoft.Office.Core) I was able to run the code successfully.

I'm using
VStudio 2003 v7.1.3088
.Net framework v1.1.4322 SP1
Excel 2002(10.6789.6735) SP3

HTH
Paul
 
I

Ian Dunn

Paul said:
After adding "Imports Excel=Microsoft.Office.Interop.Excel" and adding a
reference to the Excel 10 object model (which auto-includes
Microsoft.Office.Core) I was able to run the code successfully.

I'm using
VStudio 2003 v7.1.3088
.Net framework v1.1.4322 SP1
Excel 2002(10.6789.6735) SP3

That's the exact same version of VStudio and .Net framework that I'm
using so it's odd that it doesn't work for me.

The only difference is that your using Excel 2002. But I tried it with
Excel 2000 (Excel 9 object model) and Excel 2003 (Excel 11 object model)
on two different computers and both had the same problem.

Thanks for trying anyway.
 
R

R. MacDonald

Hello, Ian,

FWIW, I see the same behaviour as you report. I'm using:

VStudio 2003 v7.1.3088
.NET Framework version 1.1.4322 SP1
Excel 2000 (9.0.6926) SP3

You can get a reference to the existing Excel "process" by using:

Process.GetProcessesByName("Excel")

but I don't know how to use this to get a reference to the application.
But maybe someone else does.

Cheers,
Randy
 

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