Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    system tray (2002 (10.2616.2625))

    Here is a question for you. I want to be able to look at the system tray and see if a program is running

    1/ if it is already running to switch to it
    2/ if not running to start it

    any ideas.......
    thanks

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: system tray (2002 (10.2616.2625))

    By "program" do you mean an Access application, Access itself, or some other application? You might want to take a look at the thread that starts with <post#=387827>post 387827</post#>, which asks a similar question about VB6 apps.
    Charlotte

  3. #3
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: system tray (2002 (10.2616.2625))

    Sorry I ment taskbar
    I want access to look for a running program......not for itself...... lets say Outlook Express....

    if it is running then switch to outlook express
    if it is not already running then start it....

  4. #4
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: system tray (2002 (10.2616.2625))

    Here is some code that does the job for me.

    I have set it up to run from a command button. Put the top stuff at the top of the form module, then attach the other code to the command button.

    The IsOen function takes the actual name of the window as its argument. So to test for another program, run the program and note the name that appears at the top of the window.

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

    Private Function IsOpen(strCaption As String) As Boolean
    IsOpen = FindWindow(vbNullString, strCaption)
    End Function



    Private Sub Command0_Click()
    Dim retval As Variant
    If Not IsOpen("Outlook Express") Then
    retval = Shell("C:Program FilesOutlook ExpressMSIMN.EXE", vbMaximizedFocus)
    Else
    AppActivate ("Outlook Express")
    End If

    End Sub
    Regards
    John



  5. #5
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: system tray (2002 (10.2616.2625))

    Ok, I tried your snippet and the first part of opening the program if it is not already running works great
    the second part thought still has a bug in it somewhere.
    it can see that there the program is running and it switches to the program in the taskbar but the program stays minimized.

    and ideas..... I have added the code I am using from the form....
    the 3 lines that are commented out are the first way I had the program start.... I just left them there for you to see...


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

    Private Function IsOpen(strCaption As String) As Boolean
    IsOpen = FindWindow(vbNullString, strCaption)
    End Function

    Private Sub Close_Click()
    On Error GoTo Err_Close_Click

    Dim stDocName As String
    Dim retval As Variant
    ' Dim stAppName As String

    If Not IsOpen("MRP") Then
    retval = Shell("S:MANUmrp.pif", vbMaximizedFocus)
    ' stAppName = "S:MANUmrp.pif"
    ' Call Shell(stAppName, 1)
    Else
    AppActivate ("MRP")
    End If

    stDocName = "Exit"
    DoCmd.RunMacro stDocName

    Exit_Close_Click:
    Exit Sub

    Err_Close_Click:
    MsgBox Err.Description
    Resume Exit_Close_Click

    End Sub

  6. #6
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: system tray (2002 (10.2616.2625))

    I find this in Help:
    "The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized."

    So if you have switched to Access and left the other application window on the screen, you will return to it the way you left it. If you minimised it, it will stay minimised.

    I don't much about API calls, but I have looked around and found this.

    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

    Private Function FindShellWindow() As Long
    Dim hWnd As Long
    On Error Resume Next

    hWnd = FindWindow(vbNullString, Text1.Text)

    If hWnd <> 0 Then
    FindShellWindow = hWnd
    End If

    End Function


    Then this code behind a button

    Dim strWindowName As String
    Dim hWnd As Long

    strWindowName = "Command prompt"
    AppActivate strWindowName, True
    hWnd = FindWindow(vbNullString, strWindowName)

    Call ShowWindow(hWnd, 3)



    In my testing this will activate then maximize my command window.

    There are a number of constants accepted by Showwindow telling it what you want done to the window. For some reason I kept getting errors when I tried to declare then use

    Public Const SW_SHOWMAXIMIZED = 3 etc, so I just put 3 straight into the function call.

    I found this stuff here:
    http://www.experts-exchange.com/Programmin...Q_20012035.html
    Regards
    John



  7. #7
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: system tray (2002 (10.2616.2625))

    Thanks a lot John,

    That little bit worked like a charm... I though I would post the entire VB so that other people who want to try doing this would have an easy time of it.


    Option Compare Database
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

    Private Function FindShellWindow() As Long
    Dim hWnd As Long
    On Error Resume Next

    hWnd = FindWindow(vbNullString, Text1.Text)

    If hWnd <> 0 Then
    FindShellWindow = hWnd
    End If

    End Function
    Private Function IsOpen(strCaption As String) As Boolean
    IsOpen = FindWindow(vbNullString, strCaption)
    End Function

    Private Sub Close_Click()
    On Error GoTo Err_Close_Click

    Dim stDocName As String
    Dim retval As Variant
    Dim stAppName As String
    Dim strWindowName As String
    Dim hWnd As Long

    If Not IsOpen("MRP") Then
    retval = Shell("S:MANUmrp.pif", vbMaximizedFocus)
    Else
    strWindowName = "MRP"
    AppActivate strWindowName, True
    hWnd = FindWindow(vbNullString, strWindowName)

    Call ShowWindow(hWnd, 3)
    End If

    Exit_Close_Click:
    Exit Sub

    Err_Close_Click:
    MsgBox Err.Description
    Resume Exit_Close_Click

    End Sub

  8. #8
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: system tray (2002 (10.2616.2625))

    If interested in some other examples of doing this type of thing, see attached demo database (A2K format). The 2 forms (frmApps, frmListApps) illustrate various techniques to list running applications, display or close application window, etc. FWIW, to open specified application (if closed) I'd use ShellExecute API function rather than VBA Shell command. And as has been noted, AppActivate is not very reliable way to display another open application - the most reliable method I've found is to get the application window's handle (hwnd), then call both ShowWindow and SetForegroundWindow API functions (ShowWindow by itself did not work if application window was maximized, though did seem to work if app was minimized). Demo includes examples of various methods you can use to enumerate open windows, child windows, etc, which can be useful when working with other running applications.

    HTH
    Attached Files Attached Files

  9. #9
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: system tray (2002 (10.2616.2625))

    Thanks for that Mark, I will look at it with interest.
    Regards
    John



  10. #10
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: system tray (2002 (10.2616.2625))

    I am in awe.....

    thanks for the examples..... A little over my head but now I have something to pick apart and see how you did it.... thanks again......

Posting Permissions

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