Results 1 to 6 of 6

Thread: Is word running?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    I have developed a small application, which works fine for the most part. The problem begins when it tries to open Word or Excel. If they are not already opened everything is fine, but if user opened Word or Excel (and of course didn't close them) before running my program it crashes basically (not everytime but usually it does). So here are my Q's:
    1) how to check if Word or Excel is running. I know that I could look for window with specific caption (what I did actually), but how can I check if word or excel are in memory but are not visible?
    2) how to close them once I know that they are running (through code of course).
    I hope you get the idea of what I mean. Thanks in advance

    ------------------
    Visual Basic Programmer
    ------------------
    PolComSoft
    You will hear a lot about it.



    [This message has been edited by QWERTY (edited 02-18-2000).]

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111

    Post

    If you do an:

    AppActivate "Word"

    and and error returns it isn't running

    Good luck,

    ------------------
    DiGiTaIErRoR
    VB, QBasic, Iptscrae, HTML
    Quote: There are no stupid questions, just stupid people.

  3. #3
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122

    Post

    Hi,

    I tried the following, and it worked pretty good:

    Code:
    Dim appObj As Object
    
    On Error Resume Next
    Set appObj = GetObject(, "Word.Application")
    If Err Then
        MsgBox "Word is not running"
    Else
        MsgBox "Word will be closed!"
        appObj.Quit
    End If
    Replace "Word.Application" with "Excel.Application" for MS Excel.

    Roger

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Thanks

    ------------------
    Visual Basic Programmer
    ------------------
    PolComSoft
    You will hear a lot about it.



    [This message has been edited by QWERTY (edited 02-18-2000).]

  5. #5
    Lively Member
    Join Date
    May 1999
    Posts
    89

    Post

    here is another functon to see if an app is running, covers all of msoffice

    Function fIsAppRunning(ByVal strAppName As String, _
    Optional fActivate As Boolean) As Boolean
    Dim lngH As Long, strClassName As String
    Dim lngX As Long, lngTmp As Long
    Const WM_USER = 1024
    On Local Error GoTo fIsAppRunning_Err
    fIsAppRunning = False
    Select Case LCase$(strAppName)
    Case "excel": strClassName = "XLMain"
    Case "word": strClassName = "OpusApp"
    Case "access": strClassName = "OMain"
    Case "powerpoint95": strClassName = "PP7FrameClass"
    Case "powerpoint97": strClassName = "PP97FrameClass"
    Case "notepad": strClassName = "NOTEPAD"
    Case "paintbrush": strClassName = "pbParent"
    Case "wordpad": strClassName = "WordPadClass"
    Case Else: strClassName = ""
    End Select

    If strClassName = "" Then
    lngH = apiFindWindow(vbNullString, strAppName)
    Else
    lngH = apiFindWindow(strClassName, vbNullString)
    End If
    If lngH <> 0 Then
    apiSendMessage lngH, WM_USER + 18, 0, 0
    lngX = apiIsIconic(lngH)
    If lngX <> 0 Then
    lngTmp = apiShowWindow(lngH, SW_SHOWNORMAL)
    End If
    If fActivate Then
    lngTmp = apiSetForegroundWindow(lngH)
    End If
    fIsAppRunning = True
    End If
    fIsAppRunning_Exit:
    Exit Function
    fIsAppRunning_Err:
    fIsAppRunning = False
    Resume fIsAppRunning_Exit
    End Function

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post Yet Another Way

    Code:
            hwnd = 999
            Do While hwnd > 0
                hwnd = FindWindow("OpusApp", vbNullString)
                If hwnd > 0 Then
                    If vbCancel = MsgBox("Please close Microsoft Word and then press " _
                                & "OK to continue or Cancel to wait.", vbCritical + vbOKCancel, _
                                  "Warning") Then
                        Exit Sub
                    End If
                End If
            Loop

Posting Permissions

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



Click Here to Expand Forum to Full Width