PDA

View Full Version : Longstanding code to check for office apps causing issues.



Agent Smith
10-08-2020, 08:13 AM
I have a function in an access form that checks if various office apps are already running:



Function IsAppRunning(appName As OfficeAppName) As Boolean


On Error GoTo NotRunning


Dim officeApp As Object
Dim appString As String


' assume true
IsAppRunning = True


' determine which app is being checked
Select Case appName
Case 1 ' Outlook
appString = "Outlook"
Case 2 ' PowerPoint
appString = "PowerPoint"
Case 3 ' Excel
appString = "Excel"
Case 4 ' Word
appString = "Word"
Case 5 ' Publisher
appString = "Publisher"
Case 6 ' Access
appString = "Access"
End Select


' try to hook into existing object
Set officeApp = GetObject(, appString & ".Application")


ExitProc:
Exit Function


NotRunning:
IsAppRunning = False
Resume ExitProc


End Function

Recently it is causing my form to freeze on some occasions. It works for a while and then starts acting up. I have to use task manager to quit access. Restart PC and it all good for a while until it freezes again. I have been using this code for a few years without issue.

Does anyone know what could be going wrong?

Agent Smith
10-08-2020, 10:40 AM
Sorry for some reason this is in outlook section but I was certain I had posted in Access. Is it possible to move it?

Agent Smith
10-08-2020, 11:08 AM
Sorry to reply to my own thread but I've given thus a try and will see how it goes:


Function IsAppRunning(appName As OfficeAppName) As Boolean

Dim appString As String
Select Case appName
Case 1 ' Outlook
appString = "Outlook.exe"
Case 2 ' PowerPoint
appString = "PowerPnt.exe"
Case 3 ' Excel
appString = "Excel.exe"
Case 4 ' Word
appString = "WINWORD.EXE"
Case 5 ' Publisher
appString = "MSPUB.EXE"
Case 6 ' Access
appString = "msAccess.exe"
End Select


IsAppRunning = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & appString & "'").count > 0


End Function

OBP
10-10-2020, 12:02 PM
I note that your error trap does not tell you why it has found an error.
It migh be useful to add
msgbox Err.Description

SamT
10-10-2020, 05:58 PM
I knew I had seen it somewhere...

https://www.mrexcel.com/board/threads/find-a-system-process-by-its-name-and-check-whether-or-not-it-is-running.484650/post-2393395