Results 1 to 7 of 7
  1. #1
    mbar is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2011
    Posts
    12

    Maximize another application window from access form

    Hi- I have tried a long time to research this and properly execute correct code but can't get close.

    I have another program that is running (and minimized) while a user uses access. I want the last command that the user clicks to maximize the other program. I've tried findwindow() but I can't either get the syntax correct or something else that is way beyond my scope of knowledge. Getobject works with office applications but this program is not MSOffice. I would have thought this would be so straight-forward but can't even get close. Most information online is to maximize other office apps which is not the case here. Please HELP!!!

  2. #2
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    You will have to use the Windows API to perform functions on another window. VBA only directly controls the windows inside of it, that's why "findwindow" wont' work.

    VB6 forums are usually the goto for trying to find Windows API function calls. I found this one. You may have to modify it a bit to suit your needs, and sometimes you have to add CONSTANTS to your VBA code to get things to work since not all of them exist in VBA natively.

    Code:
    Private Declare Function MoveWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    
    
    Private Function ShellIt(Path As String, WindowStyle As VbAppWinStyle, Left As Long, Top As Long, Width As Long, Height As Long)
    Dim pId As Long
    pId = Shell(Path, WindowStyle)
    
    Dim Wnd As Long, pMatchID As Long
    Wnd = FindWindowEx(0, 0, vbNullString, vbNullString)
    Do While Wnd
        GetWindowThreadProcessId Wnd, pMatchID
        If pMatchID = pId Then
            ShellIt = Wnd
            MoveWindow Wnd, Left, Top, Width, Height, True
            Exit Function
        End If
        Wnd = FindWindowEx(0, Wnd, vbNullString, vbNullString)
    Loop
    End Function

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Did Access create/launch the app? Maybe you can preserve the object by using it globally, maximizing it when needed.

  4. #4
    mbar is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2011
    Posts
    12
    Access does not create the application. I have the window title of the program. Where would I include this in the code above? Or, to be more clear, what exactly do I have to modify from the code above? Also, doesn't shell start the other application? If so, the application is already running- just need to maximize it. Thank you

  5. #5
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Honestly it's not my area of expertise. It's just a good starting point. I'd look at the MSDN articles and VB6 forums for more information about the user32.dll library.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I played around with the code and it seems to be used to create an object and then do something with it. I was able to look at some of the code and get it to iterate something. Not sure what it is loping through but getting plenty of handles returned. Maybe I can take another look at it later. Need to find the correct header to interact with the dll and debug a string (something useful), like an app name, path etc. Currently, miles away.

    Code:
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    
    Private Function ShellIt()
    Dim Wnd As Long
    Wnd = FindWindowEx(0, 0, vbNullString, vbNullString)
    Do While Wnd > 0
        Wnd = FindWindowEx(0, Wnd, vbNullString, vbNullString)
    Debug.Print "Wnd = " & Wnd
        
    Loop
    End Function

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Perhaps there is a way to iterate all open windows and retrieve a name or window title. I have done it with Windows Explorer windows but third party app windows are alluding me right now. Anyway, the following will work. The only thing is you will need to know the name of the app or the "Class", as Windows understands it. For instance, Outlook 2003 is "rctrl_renwnd32"

    Instead of FindWindowExA, which goes after child windows, the following uses FindWindowA as an entry point to the DLL. This will be the parent window. Place these functions in your module.

    Code:
    Private Declare Function fFindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal strWIndowClass As String, ByVal strWIndowClass2 As String) As Long
    Private Declare Function fShowWindow Lib "user32.dll" Alias "ShowWindow" (ByVal lngHWND As Long, ByVal lngCommand As Long) As Long
    The following will find the open Notepad window and then maximize it. Nothing fancy as far as looping, etc. just an illustration. This code can go in a click event.

    Code:
    Dim lngHandle As Long
    Dim lngTemp As Long
    lngHandle = fFindWindow("Notepad", vbNullString)
    MsgBox lngHandle
    lngTemp = fShowWindow(lngHandle, 3)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 15
    Last Post: 04-11-2015, 08:15 AM
  2. Replies: 4
    Last Post: 01-05-2014, 02:09 PM
  3. Replies: 3
    Last Post: 10-23-2013, 08:11 AM
  4. Replies: 0
    Last Post: 08-26-2008, 09:22 AM
  5. Access application to Web based application
    By admaldo in forum Access
    Replies: 0
    Last Post: 06-12-2008, 06:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums