How to Bring "External App/Programs" Window to Front using VBA ?

ajay1111

New Member
Joined
Aug 15, 2022
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
How to bring window to front using the Caption/Title of the applications/Program ? The following code is used to get the Caption/Title of the application but when when Appactivate is used, it simply activates the External program but does not bring the Program's window to front or Maximize it.
So how can we bring "External Programs" Window to Front based on Caption/Title of the applications.

VBA Code:
'App Activate
Private Declare PtrSafe Function apiGetClassName Lib "user32" Alias _
                "GetClassNameA" (ByVal hWnd As Long, _
                ByVal lpClassName As String, _
                ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function apiGetDesktopWindow Lib "user32" Alias _
                "GetDesktopWindow" () As Long
Private Declare PtrSafe Function apiGetWindow Lib "user32" Alias _
                "GetWindow" (ByVal hWnd As Long, _
                ByVal wCmd As Long) As Long
Private Declare PtrSafe Function apiGetWindowLong Lib "user32" Alias _
                "GetWindowLongA" (ByVal hWnd As Long, ByVal _
                nIndex As Long) As Long
Private Declare PtrSafe Function apiGetWindowText Lib "user32" Alias _
                "GetWindowTextA" (ByVal hWnd As Long, ByVal _
                lpString As String, ByVal aint As Long) As Long
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNEXT = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255
'App Activate

Sub xSelView()
    Dim xRg As Range
    Dim xStr As String
    Dim xStrLen As Long
    Dim xHandle As Long
    Dim xHandleStr As String
    Dim xHandleLen As Long, xHandleStyle As Long
    
    On Error Resume Next
    xHandle = apiGetWindow(apiGetDesktopWindow(), mcGWCHILD)
    Do While xHandle <> 0
        xStr = String$(mconMAXLEN - 1, 0)
        xStrLen = apiGetWindowText(xHandle, xStr, mconMAXLEN)
        If xStrLen > 0 Then
            xStr = Left$(xStr, xStrLen)
            xHandleStyle = apiGetWindowLong(xHandle, mcGWLSTYLE)
            If xHandleStyle And mcWSVISIBLE Then
                If InStr(1, xStr, "Powerpoint") <> 0 Then   'Activate Powerpoint window
                AppActivate (xStr)
                Exit Sub
                End If
            End If
        End If
        xHandle = apiGetWindow(xHandle, mcGWHWNDNEXT)
    Loop
End Sub
 
One last trial...
Code:
Dim Acad As Object
Set Acad = GetObject(, "Autocad.application")
AppActivate Acad.ActiveWindow.Caption & " - " & Acad.Caption
Acad.WindowState = 1 'wdWindowStateMaximize
Dave
Didn't work ! Is it possible to get the Handle(hwnd) based on Title/Caption ie XStr and bring that hwnd application to front ?
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Ok. It's just i ask because your code would seem to only ever operate if PowerPoint is in the title/caption. There also doesn't seem to be anything in there to maximize the window- was that what you had wanted it to do?
I just wanted to maximize the window based on the Heading title ?
 

Attachments

  • Capture.JPG
    Capture.JPG
    88.1 KB · Views: 16
Upvote 0
Well let's try powerpoint then...
Code:
Dim PowPt As Object
Set PowPt = GetObject(, "PowerPoint.application")
PowPt.Visible = True
'Dim oPPTPres As Object
'Set oPPTPres = oPPT.Presentations.Add
AppActivate PowPt.ActiveWindow.Caption & " - " & PowPt.Caption
PowPt.WindowState = 1 'wdWindowStateMaximize
Seems like visible was important in the link and maybe the presentations.Add as well? Dave
 
Upvote 0
Solution
Didn't work ! Is it possible to get the Handle(hwnd) based on Title/Caption ie XStr and bring that hwnd application to front ?
This is what the APIs I referenced in my earlier reply would do. Will look at your article this afternoon.
 
Upvote 0
Just had a quick look - the API example looks like it should work, and won't be limited to COM Applications. Did you end up trying that example too?
 
Upvote 0
Well let's try powerpoint then...
Code:
Dim PowPt As Object
Set PowPt = GetObject(, "PowerPoint.application")
PowPt.Visible = True
'Dim oPPTPres As Object
'Set oPPTPres = oPPT.Presentations.Add
AppActivate PowPt.ActiveWindow.Caption & " - " & PowPt.Caption
PowPt.WindowState = 1 'wdWindowStateMaximize
Seems like visible was important in the link and maybe the presentations.Add as well? Dave
It seems to work in case of Powerpoint, but instead of full maximized window, floating window appears !
 

Attachments

  • Capture.JPG
    Capture.JPG
    87.2 KB · Views: 13
Upvote 0
Just had a quick look - the API example looks like it should work, and won't be limited to COM Applications. Did you end up trying that example too?
I did try the API example and it did work for powerpoint but i could not figure out how can i modify it to work for other applications which have Title/Process like "Autocad-Sample". The thing is my mentioned code can get Title/Process name based on initials given (like Calc for Calculator) and the Code on blog can Bring windows to front. And i want to get Title/Process name based on initials and Bring that app window on front (ie Both combined).
 
Upvote 0
I did try the API example and it did work for powerpoint but i could not figure out how can i modify it to work for other applications which have Title/Process like "Autocad-Sample". The thing is my mentioned code can get Title/Process name based on initials given (like Calc for Calculator) and the Code on blog can Bring windows to front. And i want to get Title/Process name based on initials and Bring that app window on front (ie Both combined).
The blog had, in the comments, examples of sample usage and it includes partial captions. You just need to use a wildcard character * .

'Usage Examples:
' FindWindowByTitle "returning*"
' FindWindowByTitle "*parent*"
' FindWindowByTitle "*Excel"
' FindWindowByTitle "Inbox*"
' FindWindowByTitle "Document1 - Word"
 
Upvote 0
Well let's try powerpoint then...
Code:
Dim PowPt As Object
Set PowPt = GetObject(, "PowerPoint.application")
PowPt.Visible = True
'Dim oPPTPres As Object
'Set oPPTPres = oPPT.Presentations.Add
AppActivate PowPt.ActiveWindow.Caption & " - " & PowPt.Caption
PowPt.WindowState = 1 'wdWindowStateMaximize
Seems like visible was important in the link and maybe the presentations.Add as well? Dave
Yup ! It seems to work for my program now. But it have an issue that if the app is "Maximized and behind excel" then it seems to work but if the app is "Minimized" then it doesn't work. Is there a way around it ?
Error video : << unavailable video removed >>
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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