check if application open before activating

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,066
I use the following code to activate a window,

Can anyone help with a script to check first if the application is open or not so that I don't get an error message if it isn't open.

I have tried on error goto etc... but I get a msgbox popup still which interferes with my other scripts.

Code:
ActivateWindow "Internet Explorer"

I also use

Code:
appactivate "Internet Explorer"
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hello Jaye7,

This macro will match the window caption against all the running top level windows on the system and return true if found. The match can be made from a partial window caption or the complete caption. Case is ignored when matching.

If you are using a 64 bit Windows platform, let me know. This code will need to be modified for 64 bit Windows.
Code:
'Written: October 12, 2011
'Author:  Leith Ross
'Summary: Compares the supplied window caption aagainst all top level windows currently open.
'         The window caption will match if complete or partial. Case is ignored.

Private Declare Function GetDesktopWindow _
  Lib "User32.dll" () As Long
  
Private Declare Function GetWindow _
  Lib "User32.dll" _
    (ByVal hWnd As Long, _
     ByVal wCmd As Long) As Long
      
Private Declare Function GetWindowText _
  Lib "User32.dll" _
    Alias "GetWindowTextA" _
      (ByVal hWnd As Long, _
       ByVal lpSting As String, _
       ByVal nMaxCount As Long) As Long
       

Function IsWindowOpen(ByVal Window_Caption As String) As Boolean

    Dim Caption As String
    Dim CurrWnd As Long
    Dim L As Long
    
    Const GW_CHILD As Long = 5
    Const GW_HWNDNEXT As Long = 2
  
  
       ' Start with the Top most window that has the focus
         CurrWnd = GetWindow(GetDesktopWindow, GW_CHILD)
      
          ' Loop while the hWnd returned by GetWindow is valid.
            While CurrWnd <> 0
         
              ' Get Window caption
                Caption = String(64, Chr$(0))
                L = GetWindowText(CurrWnd, Caption, 64)
                Caption = IIf(L > 0, Left(Caption, L), "")
         
              ' Test if the caption matches the Window requested
                If LCase(Caption) Like "*" & LCase(Window_Caption) & "*" Then
                   IsWindowOpen = True
                   Exit Function
                End If
         
              ' Get the next Window
                CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
         
              ' Process Windows events.
                DoEvents
            Wend
    
End Function
Example of Using the Macro
Code:
    Res = IsWindowOpen("Internet Explorer")
 
Upvote 0
Sorry Leith, just a question.

Do I need a particular reference for this to work as I ran the script and nothing happens, I used the test code that you provided.
 
Upvote 0
Very nice. I been searching for a while for a easy, straight forward method and yours is the cleanest I've run across.

It is not exactly what I need but I thought that I could modify it without too many problems.

I wrote a small Excel/VBA script a while back that used the AppActivate and SendKeys commands to send commands to various pieces of equipment being monitored. I had a ListBox of commands associated with a telnet window to the equipment in question. When I clicked on the command in the ListBox it was sent to the corresponding telnet window for the piece of equipment being monitored.

As each window (at the time) had a unique Window Title I could activate the appropriate window using the AppActivate command .

I tried to use the Excel/VBA application again but the environment has changed. To access the equipment I now have to telnet to an intermediate server then telnet to the destination piece of equipment. This has me now dealing with multiple instances of telnet windows with the same Window Title. And using AppActivate with that common title always selectes the same instace of the telnet application.

What I was hoping to do was to scan through all the open applications, picking up all instances of the telnet application in there seperate windows, and populate a ListBox. I could then select the correct instance from the ListBox and use the selection as the reference in the AppActivate command. However, as each instance has the same Window Title I am again stuck.

However, if the Window Handle were returned along with the Window Title I would then be able to AppActivate the correct instance of the telnet application by using the Window Handle.

The modification that I made to your script places all identified windows into the ListBox. There are then entries in the ListBox for which I have no idea what they are for. Is there a way to limit the windows found to those that are displayed in the Task Manager's Application tab?
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top