Know if Windows Calculator is open or not

Magic_Doctor

Board Regular
Joined
Mar 18, 2009
Messages
56
Hello,
By means of an icon, to which a macro is assigned, I open the Windows calculator when I click on this icon:
VBA Code:
Sub Calculatrice()
‘Excel 2007
    Shell "c:\windows\system32\calc.exe"
End Sub
It works very well.
The problem is that if I click, for example, 10 times on the icon, 10 calculators will be opened. We can thus end up with a lot of open calculators… What I would like is that once the calculator is open, we can no longer open others. Is there a way to know if the calculator is open? Suppose this is possible, then it would suffice to assign the result to a boolean variable (CalcOpen) and write this in the macro so that there can only be one open calculator:
VBA Code:
Sub Calculatrice()
    If CalcOpen Then Exit Sub
    Shell "c:\windows\system32\calc.exe"
End Sub
Thank you in advance for all responses.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How to know (works in W10 as calculator.exe):

So if function returns true, do something. If that is to activate calculator, I don't know how.

This one might be better
 
Upvote 0
Solution
Maybe something like this?
VBA Code:
Sub Calculatrice()
If IsCalcOpen Then
   AppActivate "calculator"
   Exit Sub
End If
Shell "c:\windows\system32\calc.exe"

End Sub

Function IsCalcOpen() As Boolean
Dim objList As Object

Set objList = GetObject("winmgmts:") _
    .ExecQuery("select * from win32_process where name='calculator.exe'")

CalcOpen = objList.count > 0

End Function
Can't stop anyone from opening another instance of calculator via the Windows task bar, so the idea has limitations.
 
Upvote 0
Hello Micron,

Thanks, il works!
VBA Code:
Function IsProcessRunning(process As String) As Boolean
    Dim objList As Object
  
    Set objList = GetObject("winmgmts:") _
        .ExecQuery("select * from win32_process where name='" & process & "'")
  
    IsProcessRunning = objList.Count > 0
End Function
VBA Code:
Sub Calculatrice()

    If IsProcessRunning("calc.exe") Then Exit Sub
    Shell "c:\windows\system32\calc.exe"
End Sub
 
Upvote 0
Great! You can mark this one as solved, I guess.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top