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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Is the external program you're trying to bring to the front "Powerpoint"?
 
Upvote 0
You can trial this...
Code:
AppActivate xStr.ActiveWindow.Caption & " - " & xStr.Caption
xStr.WindowState = 1 'wdWindowStateMaximize
HTH. Dave
 
Upvote 0
You can trial this...
Code:
AppActivate xStr.ActiveWindow.Caption & " - " & xStr.Caption
xStr.WindowState = 1 'wdWindowStateMaximize
HTH. Dave
It throws the Error !
 

Attachments

  • Capture.JPG
    Capture.JPG
    49.4 KB · Views: 46
Upvote 0
No, There are multiple programs like Autocad, STAAD etc !
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?
 
Upvote 0
Is XStr the object name of the application? I see that it is declared as a string. I'm not using a 64bit installation right now to trial your code. The code I posted works for Word if XStr is the name of the Word object application. Maybe just trial replacing the XStr with Autocad? Dave
 
Upvote 0
One last trial...
Code:
Dim Acad As Object
Set Acad = GetObject(, "Autocad.application")
AppActivate Acad.ActiveWindow.Caption & " - " & Acad.Caption
Acad.WindowState = 1 'wdWindowStateMaximize
Dave
 
Upvote 0
One last trial...
Code:
Dim Acad As Object
Set Acad = GetObject(, "Autocad.application")
AppActivate Acad.ActiveWindow.Caption & " - " & Acad.Caption
Acad.WindowState = 1 'wdWindowStateMaximize
Dave
I was thinking of this exact approach if it was the case that the application was powerpoint, but now that it's any external program... there are two APIs to use, I think, but don't have them immediately to hand.
 
Upvote 0
I was thinking of this exact approach if it was the case that the application was powerpoint, but now that it's any external program... there are two APIs to use, I think, but don't have them immediately to hand.
I found a blog to VBA – Find A Window By Its Title and Bring It To The Front
But i could not understand it and use it in the stated code above. Could you take a look at it ? Maybe it could be a help ?
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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