×

注意!页面内容来自https://www.mrexcel.com/board/threads/know-if-windows-calculator-is-open-or-not.1201125/,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Know if Windows Calculator is open or not

Magic_Doctor

Board Regular
Joined
Mar 182009
Messages
56
Hello,
By means of an iconto which a macro is assignedI 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 clickfor example10 times on the icon10 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 openwe can no longer open others. Is there a way to know if the calculator is open? Suppose this is possiblethen 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.
 
How to know (works in W10 as calculator.exe):

So if function returns truedo something. If that is to activate calculatorI 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 barso the idea has limitations.
 
Upvote 0
Hello Micron,

Thanksil 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 solvedI guess.
 
Upvote 0
Back
Top