• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 80
  • Last Modified:

Check for 2 instances of Excel

In Excel VBA I wish to determine if 2 sessions/instances of Excel is open, prior to allowing a macro to continue.  I have found with some macros if 2 instances of Excel are open it has some unwanted consequences so I want to check a the beginning of the macro if 2 instances of Excel are open..
0
upobDaPlaya
Asked:
upobDaPlaya
  • 5
  • 4
2 Solutions
 
Martin LissOlder than dirtCommented:
Are you talking about two workbooks open in the same instance of Excel or actually two instances of Excel itself? I ask because I thought that with the modern versions of Excel that that wasn't possible without a registry edit.
0
 
Martin LissOlder than dirtCommented:
If it's two workbooks then you can call this little Function and it will return True or False

Function isMoreThanOneOpen() As Boolean

If Workbooks.Count > 1 Then
    isMoreThanOneOpen = True
End If

End Function

Open in new window

0
 
Dale FyeCommented:
You might want to take a look at my article on Automating Excel from Access.  This particular article focuses on the code necessary for identifying multiple instances of Excel, and enumerating each of those.

It was written for Access, and thus uses table to store info rather than arrays or hidden worksheets, but I think you will find what you are looking for in that article.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
upobDaPlayaAuthor Commented:
Its actually for two instances of Excel.  I can go to Windows/Programs, etc and actually open 2 instances of Excel not 2 workbooks.  For a particular macro I have for some reason if 2 instances of Excel is open it creates issues..
0
 
upobDaPlayaAuthor Commented:
Dale, I am not following the part in the article for "MSDN reference for IIDFromString"...Do I need to save that as a reference.  In addition can I get more clarification on what I need to do for "MSDN reference for AccessibleObjectFromWindow"  Do I need to save this as a reference to my machine...
0
 
Martin LissOlder than dirtCommented:
This piece of code that I found on the web says that it will tell you if there's more than one. I can't test it.

Sub InstanceCount()
Dim objList As Object, objType As Object, strObj$
strObj = "Excel.exe"
Set objType = GetObject("winmgmts:").ExecQuery("select * from win32_process where name='" & strObj & "'")
If objType.Count > 1 Then
MsgBox objType.Count & " Excel instances are running on your system.", , "More than one instance"
Else
MsgBox "Only this instance is running on your system.", , "One and done"
End If
End Sub

Open in new window

0
 
upobDaPlayaAuthor Commented:
Martin..I will try tomorrow and close the question.  Thanks for your patience.
0
 
Martin LissOlder than dirtCommented:
I was able to create two instances of Excel and testing showed that the code I found does work.
0
 
upobDaPlayaAuthor Commented:
It worked !
0
 
Martin LissOlder than dirtCommented:
I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now