?
Solved

Check Instance of Excel

Posted on 2006-05-10
11
Medium Priority
?
253 Views
Last Modified: 2012-05-05
I have a function that checks wether an instance of excel is already open, this works fine, but I am using the code:

    if there is no instance then I use "Set appxl = New Excel.Application"
    if there is an existing instance then I use "Set appxl = Excel.Application"

But both bits of code open a new instance, why? Any ideas/alternatives so the code will work to open a workbook in an existing instance?

Thanks
Matt

   
0
Comment
Question by:generali
11 Comments
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 16649308
And what about

Set appxl = GetObject("","Excel.Application")
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16649442
or this:

Dim appxl as Excel.Application

Public Sub CreateExcelInstance() as Excel.Application

'create a SINGLETON instance of appxl
' If it does not already exist, create it, otherwise, leave the existing instance as is

If appxl is Nothing then
   Set appxl = New Excel.Application
End If

End Sub
0
 
LVL 2

Author Comment

by:generali
ID: 16649448
I am using early binding so would preferebly like to keep all my code this way, but I would use that as a last resort...Ill give it a go.

Cheers
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 2

Author Comment

by:generali
ID: 16649501
That defeats the point, I'm trying to reference the current instance of excel if it exists, what you have put there is just saying open a new instance if one doesn't exist, sorry I might not be explaining the very well, if there is an instance of excel open then I want to open a workbook....but how do I make a reference to this instance without creating a new one, hence why i thought the following code would work ('new' creating a new instance and the other just creating a reference to the existing one)

    if there is no instance then I use "Set appxl = New Excel.Application"
    if there is an existing instance then I use "Set appxl = Excel.Application

Thanks
Matt
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 16649502
Doesn't stop you using early binding.
There's nothing wrong with

Dim appxl as Excel.Application

Set appxl = GetObject(,"Excel.Application")
0
 
LVL 2

Author Comment

by:generali
ID: 16649569
I know, thanks for that it does work, the points are yours, but just out of curiosity, is there a way using early binding? Its really doing my nut in !

Thanks
Matt
0
 
LVL 44

Accepted Solution

by:
Leigh Purvis earned 1000 total points
ID: 16649763
Dim appxl as Excel.Application
is using early binding.  That's what I was saying.

In early binding it's considered more traditional to use the New keyword to create your instance rather than say  CreateObject - but how you get the object isn't key.
In late binding you have no choice.  
But GetObject applies to both just fine.

It's to do with when you're exposing the object model - not how you obtain a pointer to one.
0
 
LVL 27

Expert Comment

by:jjafferr
ID: 16963401
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Split: Arthur_Wood {http:#16649442} & LPurvis {http:#16649502}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

jjafferr
EE Cleanup Volunteer
0
 
LVL 2

Author Comment

by:generali
ID: 16966952
Cheers Lpurvis, sorry for not getting back to you sooner

Matt
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 16967539
No probs.

Arthur was instructive here to was he not? :-)
0
 
LVL 2

Author Comment

by:generali
ID: 16967567
Nope, not as instructive as you, so the points are yours rather than his. If you look at my comments after his you will see that what he put was not what i was after anyway : )

Matt
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question