PDA

View Full Version : Test if more than one instance of excel is open



Djblois
01-07-2009, 10:14 AM
I was wondering if it was possible to check if there is more than one instance of Excel open. I don't want to check if there is more than one workbook open, I want to check if Excel is opened twice. I have looked in my books and did a search on these forums. Thank you for all your help

georgiboy
01-07-2009, 11:07 AM
Have a look at this, its simple but it seems to work for me.

MsgBox Windows.Count

Djblois
01-07-2009, 11:24 AM
georgiboy,

Thank you for your help. Unfortunately it doesn't work for me-it counts how many books are open but not how many instances of Excel are open.

vzachin
01-07-2009, 11:55 AM
check out this link
http://www.vbaexpress.com/forum/showthread.php?t=16485

and modify accordingly

Bob Phillips
01-07-2009, 12:02 PM
This seems to work



Public Function FindInstances(ByVal ProcName As String) As Long
Dim mpWMIService
Dim mpProcesses
Dim mpComputer As String

mpComputer = "."
Set mpWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
mpComputer & "\root\cimv2")
Set mpProcesses = mpWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & ProcName & "'")
FindInstances = mpProcesses.Count
Set mpWMIService = Nothing
End Function

Djblois
01-07-2009, 12:12 PM
xld,

I only had one excel open and it still told me I had 3 open. I am looking at that code and I have no idea what it is doing for me to try to fix it on my own. Can you give me an idea of what it is doing so I can learn?

georgiboy
01-07-2009, 12:22 PM
Sorry to jump in here but if Windows.Count is counting workbooks then what is Workbooks.Count Doing?

I thought you could have multiple workbooks open in one window but each time there is a new window that was a new instance of excel.

jfournier
01-07-2009, 01:12 PM
I've attached some code I wrote a while back to count the number of instances of any executable name, so you can use this to count the number of Excel.exe instances there are. This won't give you programmatic access to each of the Excel applications though, I don't know if that's really possible...

Bob Phillips
01-07-2009, 03:18 PM
xld,

I only had one excel open and it still told me I had 3 open. I am looking at that code and I have no idea what it is doing for me to try to fix it on my own. Can you give me an idea of what it is doing so I can learn?

What did you pass as ProcName?