Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

Thread: Useing AppActivate

  1. #1
    Guest

    Default

    How would I modify this to work more like a [control-TAB]
    If the app is already open and to open app if not already?

    Sub ardis_open()
    Range("A4:J93").Copy
    Range("A2").Activate
    MyAppID = Shell("C:ardiscowin.EXE", 1) ' Run ardis
    AppActivate MyAppID ' Activate ardis
    End Sub

  2. #2
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Anon,

    Here's one way to do it, copy the following into a module (I added the findwindow function so that some workable code is in one post):

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    sub test()
    h = FindWindow("Windows Class Name for your Application", vbNullString)
    If h > 0 Then
    AppActivate "Windows Class Name for your Application"
    Else: MyAppID = Shell("C:ardiscowin.EXE", 1)
    end if
    end sub

    When a program is launched, like Lotus Notes or Winzip, Windows assigns an integer which it calls a "handle" by nickname. For Lotus Notes, it is "Notes." You'll have to find the Windows Class name for your program you're referring to. If the associated integer for the handle is greater than zero, the program is open. The best way I've found to test for a launched program.

    Here's a small list of class names and their respective applications:

    http://www.generation.net/~hleboeuf/handlevb.htm

    And here's how to score other class names:

    http://support.microsoft.com/default...;EN-US;q112649

    Cheers, Nate

    [ This Message was edited by: NateO on 2002-02-27 16:10 ]

  3. #3
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    [quote]
    On 2002-02-26 14:09, NateO wrote:
    Anon,

    Here's one way to do it:

    sub test()
    h = FindWindow("Windows Class Name for your Application", vbNullString)
    If h > 0 Then
    AppActivate "Windows Class Name for your Application"
    Else: MyAppID = Shell("C:\ardis\cowin.EXE", 1)
    end if
    end sub


    Nate
    You forgot the windows API for this sub

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    The windows handle is usually a long

    The good links that NateO gave are useful
    if you are using VB but for VBA you will
    need to change a few things for the Ms link..
    Use the IEtimer as VBA has no Timer.
    You will have to link the Prints to a textbox
    If you can do these then the routine is very good.
    Otherwise to get the class name have a look
    @ NateO 2nd link or use this routine I use

    Option Explicit
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long


    Sub GetClass()
    Dim WinWnd As Long, sWndTitle As String, RetVal As Long, lpClassName As String

    sWndTitle = InputBox("Enter the exact window title:" & Chr$(13) & Chr$(10) & _
    "Note: must be an exact match")

    WinWnd = FindWindow(vbNullString, sWndTitle)
    If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub

    lpClassName = Space(256)
    RetVal = GetClassName(WinWnd, lpClassName, 256)
    MsgBox "Classname for Window Caption" & vbLf & vbLf & _
    "[" & sWndTitle & "] = " & Left$(lpClassName, RetVal)

    End Sub





    Ivan F Moala




  4. #4
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Ivan, good catch on the API, my fingers are moving faster than the brain. Another good one with the VB. I liked your code for calling the class name, very choice, hope you don't mind me playing with it below...While it seems relatively simple, I was having a little difficulty getting the exact string correct. So I played with a function (er 2) where you can enter part of a string to return the class name (i.e., Mr Excel or Internet returns IEFrame). It likes to default to the program manager when all else fails, but otherwise, I like this a lot. Thought you might be interested:

    Option Explicit
    Option Compare Text
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private psAppNameContains As String
    Private pbFound As Boolean
    Private sTitle As String
    Public Function apptitlebystringpart(StringPart As String) As Boolean
    Dim lRet As Long
    psAppNameContains = StringPart
    lRet = EnumWindows(AddressOf CheckForInstance, 0)
    apptitlebystringpart = pbFound
    pbFound = False
    End Function
    Private Function CheckForInstance(ByVal lhWnd As Long, ByVal _
    lParam As Long) As Long
    Dim lRet As Long
    Dim iNew As Integer
    If Trim(psAppNameContains = "") Then
    CheckForInstance = False
    Exit Function
    End If
    sTitle = Space(255)
    lRet = GetWindowText(lhWnd, sTitle, 255)
    sTitle = StripNull(sTitle)
    If InStr(sTitle, psAppNameContains) > 0 Then
    CheckForInstance = False
    pbFound = True
    Else
    CheckForInstance = True
    End If
    End Function
    Private Function StripNull(ByVal InString As String) As String
    Dim iNull As Integer
    If Len(InString) > 0 Then
    iNull = InStr(InString, vbNullChar)
    Select Case iNull
    Case 0
    StripNull = InString
    Case 1
    StripNull = ""
    Case Else
    StripNull = Left$(InString, iNull - 1)
    End Select
    End If
    End Function
    Sub GetClass()
    Dim WinWnd As Long, sWndTitle As String, RetVal As Long, lpClassName As String
    sWndTitle = InputBox("Enter at least one word from the Window Title:")
    If sWndTitle <> "" Then
    apptitlebystringpart (sWndTitle)
    sWndTitle = sTitle
    WinWnd = FindWindow(vbNullString, sWndTitle)
    lpClassName = Space(256)
    RetVal = GetClassName(WinWnd, lpClassName, 256)
    MsgBox "Classname for Window Caption" & vbLf & vbLf & _
    "[" & sWndTitle & "] = " & Left$(lpClassName, RetVal)
    Else: MsgBox ("You didn't enter a term, making the task challenging...")
    End If
    End Sub

    This type of procedure is almost necessary if you're going to try to use the call names of programs like Adobe Acrobat or Paint Shop Pro, where the call name is "dynamic" (Windows changes the call name every time it's launched).


    Cheers,

    Nate

    [ This Message was edited by: NateO on 2002-02-27 16:09 ]

  5. #5
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Nate

    Good one....was actually going to code
    one to look at the windows name (partial)

    No need to now....and yes this is much better
    then typeing in the exact name of the window


    cheers

    Ivan

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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