469,591 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

Accessing existing Excel instance

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

--
Ian Dunn
Polyhedron Software Ltd. - www.polyhedron.com
Programs for Programmers - QA, Compilers, Tools, Graphics, Consultancy
Jul 4 '06 #1
3 2732

"Ian Dunn" <ia******@polyhedron.comwrote in message
news:P9********************@eclipse.net.uk...
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
Jul 5 '06 #2
Paul Larson wrote:
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.

--
Ian Dunn
Polyhedron Software Ltd. - www.polyhedron.com
Programs for Programmers - QA, Compilers, Tools, Graphics, Consultancy
Jul 5 '06 #3
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
Ian Dunn wrote:
Paul Larson wrote:
>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.
Jul 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Josh Behl | last post: by
1 post views Thread by Fred Morrison | last post: by
14 posts views Thread by johnsobd | last post: by
3 posts views Thread by =?Utf-8?B?dmJ0cnlpbmc=?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.