473,728 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Applicati on
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = GetObject(, "Excel.Applicat ion")
oWB = oXL.ActiveWorkb ook
oSheet = oWB.ActiveSheet

Unfortunately this doesn't work. What happens is that oXL.ActiveWorkb ook
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.Applicat ion")
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 3028

"Ian Dunn" <ia******@polyh edron.comwrote in message
news:P9******** ************@ec lipse.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.Applicati on
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = GetObject(, "Excel.Applicat ion")
oWB = oXL.ActiveWorkb ook
oSheet = oWB.ActiveSheet

Unfortunately this doesn't work. What happens is that oXL.ActiveWorkb ook 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.Applicat ion")
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.Offic e.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.67 35) 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.Offic e.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.67 35) 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.GetProc essesByName("Ex cel")

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.Offi ce.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.67 35) 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
1318
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 up each order number and return infomration relating to the order from SQL. Can anyone point me to the right process?
2
30576
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 as "OutputStampTime". I am facing problem while accessing this cell value. When I open excel, I can see the value as "6:49 AM" . But when I click on that cell in the formula bar I am seeing as 01/27/2005 06:49:47 AM.
4
3197
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 property to 'true'...then I open the workbook in question via my code..... However, when I try this using ASP.NET. It does not work. I've messed around and gave full access and control to my ASPNET user to practically every darn folder on...
1
8371
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 suppress error handler Set xlApp = GetObject(,"Excel.Application") On Error GoTo PROC_ERR ' resume normal error handling If xlApp Is Nothing Then
14
2407
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 the table, but VB.NET will not see any fields past the first nine. I have discovered that the problem is the 10th field name begins with a 1. I was able to create a copy of this table, rename the field and can successfully access it. The...
2
2184
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 = objExcel.ActiveWorkbook.ActiveSheet The problem is that every time I need to export, it results in creating
1
1283
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: Dim _objXLApp As Excel.ApplicationClass Dim _objXLFile As Excel.Workbook Dim _objWorkSheets As Excel.Worksheets
2
1407
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 purpose of this task is so that if a user hits ctrl+n in IE, the new window is "aware" of the other instance. Thanks in advance,
3
1816
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 on the status of a project that our company is working on. Programmatically, if the status changes, I want to relay that information to the 'lblWorkStatus' control on the main form. My code looks like this:
0
8891
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9406
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9261
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9185
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8106
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
2641
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.