472,364 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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 2902

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim | last post by:
Hi, I have a need to process a column of data and get information from SQL 2000 and return it to the Excel Spread Sheet. The data will be a list of order numbers in column 1. I need to look...
2
by: Kumar | last post by:
Hi Folks, I have a question regarding my windows c# application. This application just reads MS Excel file and puts the data in to sql server database. In that excel file ,it has one named cell...
4
by: Josh Behl | last post by:
When I try to programmatically open an existing Excel document using a custom windows form, it works perfectly. I instanciate a new instance of the Excel.ApplicationClass and then set the Visible...
1
by: Fred Morrison | last post by:
In VB6, I would check to see if Excel was already running by this technique Private m_booExcelCreatedHere As Boolean Dim xlApp as Excel.Application On Error Resume Next ' temporarily...
14
by: johnsobd | last post by:
I am accessing an existing Foxpro database, of which I have no control over. I have been successful in accessing all the tables except for one. This one table I can access the first 9 fields in...
2
by: leeedw | last post by:
Hi everyone, I use the following code whenever I export to Excel from VB.NET objExcel = New Excel.Application() objWorkBooks = objExcel.Workbooks objWorkBook = objWorkBooks.Add objWorkSheet...
1
by: Lucile | last post by:
Hi all, I am trying to open a Excel Workbook to check data in worksheets. The ONLY info I have about the EXCEL file is its entire path. I tried to do the following, but an exception occurs: ...
2
by: ctrl.undo | last post by:
Hi, I'm not sure if this is possible, but I'm looking for a way to access another IE browser window using JavaScript (assuming the two browser windows are running in the same instance). The...
3
by: =?Utf-8?B?dmJ0cnlpbmc=?= | last post by:
Background: I have a windows form called 'frmMain'. On this form, I have a LABEL control called 'lblWorkStatus', and it's property is set to PUBLIC. On another form, I have a process that checks...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.