VBA Code to move windows via call to API

leighhobson89

New Member
Joined
Aug 25, 2016
Messages
36
Good afternoon,

Can anyone provide me with the code required to find the screen co-ordinates of any window, searchable by its window name as it would appear in appActivate() for example? I know nothing about using APIs to find windows, nor handles etc., and the numerous threads I have read make no sense to me. I am looking for a copy and paste solution to move a stubborn window by VBA, that opens exactly in the center of the screen irrespective of the resolution, and maximising and sizing is disabled on it. Incidentally most keyboard shortcuts cannot be used in it either which is the reason for this, so i can simulate mouse clicks instead once i set the location of the window. (I will need help on that too eventually I guess :(

I need this for 64bit and 32bit API libraries, as it will be used on different versions of excel (i.e. one function fits all via If <64 bit> Then... : Else <32 bit>... ).

It is a big ask, but if anyone has a spare hour it would be a huge help, as I am blocked from completing my macro and can't get my head around this topic at all.

I should add that Application.Top, Application.Left etc don't work for this external window.

e.g.

Code:
Sub MoveWindow(posLeftCurrent As Integer, posTopCurrent As Integer, posLeftNew As Integer, posTopNew As Integer, windowNameToMove As String)

...CODE

End Sub

To be called with something like:

Code:
...
appActivate("Untitled - Notepad")
MoveWindow(400, 200, 0, 0, "Untitled - Notepad")
...

Yours Hopefully!!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Morning Leigh,

Question...

Is it vital that you retrieve the original positioning of the window, or is simply moving it your aim?

Moving a window is simple enough once you have the found the windows handle (or "hWnd") by using the SetWindowPos function to set the window position. I'm almost certain there's a few other ways to do it, but this is the one I've always used. Retrieving a windows coordinates is trickier, but not impossible.

Thrown a quick example together, admittedly not exactly what you asked for but hopefully helpful for pointing you in the right direction. Should function on both 64 and 32 bit platforms.

Pop the following in a new module.

Code:
 Option Explicit

Declare PtrSafe Function SetWindowPos Lib "user32" ( _
    ByVal hwnd As LongPtr, _
    ByVal hWndInsertAfter As LongPtr, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal cx As Long, _
    ByVal cy As Long, _
    ByVal wFlags As Long) As Long

Declare PtrSafe Function FindWindow Lib "user32" _
    Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As LongPtr

Public Const HWND_TOP = 0  '//moves to top of Zorder
Public Const SWP_NOSIZE = &H1  '//Overwrites cx & cy to not resize window.

Sub MoveMe()
    Dim hwnd As LongPtr
    
    hwnd = FindWindow(vbNullString, "Calculator") 'Find the handle of the window based on the title, in this case "Calculator". 

    hwnd = SetWindowPos(hwnd, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE) 
    '^ moves the window. note the two 100's, these are the X and Y positions of the window. the SWP_NOSIZE stops the window size being adjusted.
    
End Sub
 
Upvote 0
You sir, are a mind-reader! The app I am using is so stubborn and I couldnt find a way to move it. In fact it is so stubborn that there are no keyboard shortcuts in it i.e. tabbing to navigate between elements. So I wanted to move it to the top corner, as I will now be attempting to write something that can simulate mouse clicks in my macro. Any tips for that?

e.g. left click in x=121, y=96 (co-ordinates are variable as may be called a few times) - I guess this will use Windows API also, which I know nothing about.

Thanks so much for your help; if you are too busy, I will post this as a new thread that better describes it in the title, so others could help.
 
Upvote 0
EDIT : I did the mouse part myself. I want to thank you for helping me with this difficult code, I feel much more confident with the concept of API calls to Windows Libraries now. :)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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