determine word is open?

Happy YN

Registered User.
Local time
Today, 20:16
Joined
Jan 27, 2002
Messages
425
How do you determine if the word application is open or not?
How can you close it if it is?
Thanks
 
Not sure if this is what you need or not - I found this a while ago while searching out a similar problem and kept it for future reference!!

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 = vbNullString
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
 
Confusion said:
Not sure if this is what you need or not - I found this a while ago while searching out a similar problem and kept it for future reference!!

You would need to include the other api functions included in that code:

  • apiFindWindow
  • apiSendMessage
  • apiShowWindow
  • apiSetForegroundWindow

Otherwise it will never work.
 
That question has been asked and answered on quite a few posts. Searching this forum is a great way to find the answers to your quests.

API: Close another Application

For those in the need... this will open word if it is not already open or it will set the focus to the Word window if it is already open...

Code:
Public Function OpenCalculator_Click()
On Error GoTo Err_OpenCalculator_Click
    
    'AppActivate "Calculator", False 'Programs Title Bar Name
    AppActivate "Word", False 'Programs Title Bar Name
    
Exit_OpenCalculator_Click:
    Exit Function
    
Err_OpenCalculator_Click:
    If Err.Number = 5 Then 'Invalid procedure call or argument
        'Call Shell("C:\Windows\System32\calc.exe", vbNormalFocus)
        Call Shell("C:\Program Files\Microsoft Office\Office11\WINWORD.EXE", vbNormalFocus)
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_OpenCalculator_Click
    End If
    
End Function
 
Thanks all for your replies - very overwhelming

I don't know about the closing down part but I didn't notice anyone answering how one can tell its open so I've devised this simple test

Code:
Dim wrdTmp As Word.Application
   On Error Resume Next

 Set wrdTmp = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
   MsgBox "not open", vbOKOnly + vbDefaultButton1
            Else
            MsgBox "open", vbOKOnly + vbDefaultButton1
            End If

This seems to work - have I missed anything?
Thanks
 

Users who are viewing this thread

Back
Top Bottom