Consulting

Results 1 to 5 of 5

Thread: Longstanding code to check for office apps causing issues.

  1. #1

    Longstanding code to check for office apps causing issues.

    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?

  2. #2
    Sorry for some reason this is in outlook section but I was certain I had posted in Access. Is it possible to move it?

  3. #3
    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

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,192
    Location
    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

  5. #5
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,302
    Location
    I knew I had seen it somewhere...

    https://www.mrexcel.com/board/thread...0/post-2393395
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •