Examples for Visual Basic for Application accessing the Windows API: Bring window to the top
This example uses EnumWindows to find a window by its caption and then calls ShowWindow and SetForeGroundWindow to bring it to the top.
This example example needs the VBA declarations of the Windows API which can be found here.
option explicit
global captionPart_ as string
sub main(captionPart as string)
captionPart_ = captionPart
call EnumWindows(addressOf EnumWindowsProc, byVal 0&)
'
' If called from the visual basic editor, sleep for a second
' so as to see the effect of the procedure
'
call Sleep (1000)
end sub
function EnumWindowsProc(byVal hWnd as long, byVal lParam as long) as long ' {
dim windowText as string
dim windowClass as string * 256
dim retVal as long
windowText = space(GetWindowTextLength(hWnd) + 1)
retVal = GetWindowText(hWnd, windowText, len(windowText))
windowText = left$(windowText, retVal)
if inStr(1, windowText, captionPart_, vbTextCompare) then
call ShowWindow(hWnd, SW_SHOW)
call SetForeGroundWindow(hWnd)
'
' We have found a Window, the iteration
' process can be stopped
'
EnumWindowsProc = false
exit function
end if
EnumWindowsProc = true
end function ' }
goWnd.ps1 is a PowerShell script that takes as its only argument the name of a process and then calls the WinAPI function SetForegroundWindow() with the process's main window handle.