Search notes:

Examples for Visual Basic for Application accessing the Windows API: SetWindowsPos

This example starts notepad with ShellExecute and finds its handle with FindWindow and then places the notepad window to the topmost position using the SetWindowPos WinAPI call.
This example needs the VBA declarations of the Windows API which can be found here.
option explicit

sub main()

    dim hWndNotepad as long

    call ShellExecute(0, "Open", "notepad.exe", ""    , "", 1)

  '
  ' Sleep a short while to give notepad.exe time to start and initialize.
  ' Otherwise, the following FindWindow would be too early and not find
  ' any window.
  '
    call Sleep(100)

  '
  ' The first argument of FindWindow expects a class name.
  ' For notepad, the class name is notepad.
  '
    hWndNotepad = FindWindow("notepad", vbNullString)

    if hWndNotepad = 0 then
       msgBox "could not find notepad window"
       exit sub
    end if

  '
  ' Put the window to the topmost position:
  '
    call SetWindowPos(hWndNotepad, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)

end sub
Github repository WinAPI-4-VBA, path: /examples/SetWindowPos/HWND_TOPMOST.bas

See also

Other examples

Index