How to move Word cursor? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
leihu
Posts: 51
Joined: 19 Mar 2020, 22:08

How to move Word cursor?

23 Jan 2024, 00:16

Dear Experts,

I've created a hotkey to scroll Word documents. It scrolls the text and the cursor together. After scrolling, if I hit any arrow key, the document is scrolled back to its original location where the cursor was. You can mimic this by turning the mouse wheel, and then hit any arrow key (Word must be in web layout view).

Is there any COM method (or any method) to move the cursor to the current view without scrolling back the text?

Thanks in advance!

HL
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: How to move Word cursor?

23 Jan 2024, 00:55

Try using bookmarks. To set a bookmark use:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.activedocument.Bookmarks.Add("test")
to go to a bookmark:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.Selection.Goto(-1,,,"test")
14.3 & 1.3.7
leihu
Posts: 51
Joined: 19 Mar 2020, 22:08

Re: How to move Word cursor?

23 Jan 2024, 06:58

Hi @flyingDman,

Thanks for your prompt reply. But, this is not what I want. I don't have a specific location to bookmark and go back later. I want the cursor (insertion point) that is scrolled away with text to be moved to the current view port, wherever it is.

Any idea?

Thanks!
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: How to move Word cursor?

23 Jan 2024, 19:00

leihu wrote:
23 Jan 2024, 00:16
I've created a hotkey to scroll Word documents. It scrolls the text and the cursor together. After scrolling, if I hit any arrow key, the document is scrolled back to its original location where the cursor was. You can mimic this by turning the mouse wheel, and then hit any arrow key (Word must be in web layout view).

Is there any COM method (or any method) to move the cursor to the current view without scrolling back the text?

In general it is a good idea to post your code. It does not seem likely that your script "scrolls the text and the cursor together" as pushing arrow down causes the view to more to where the cursor is, which apparently is still at its original location.

I know of no way to make the cursor move to within the current view with COM. What text is currently within the view is not information that COM can provide. The window could be sized down so that only a single word or no words at all are in view.

You might be able to achieve it outside of COM by having AHK click in the center of the Word window which would hopefully move the cursor to within the current view, then use COM to scroll to the current cursor position.

Or you might rework your scrolling script to actually move the cursor and view together.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
leihu
Posts: 51
Joined: 19 Mar 2020, 22:08

Re: How to move Word cursor?

24 Jan 2024, 11:39

Hi @FanaticGuru,

The code for scrolling the Word doc doesn't have much to do with this question. Clicking the centre of the Word window is not acceptable as this also moves my mouse cursor. Also, I don't like moving the cursor and view together because most of time I don't want the insertion point to change. But thank you very much for your reply and suggestions.

If you think there's no way to move the cursor to the current view using COM or any other methods, I'll give up.

Thanks once again!

HL
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: How to move Word cursor?  Topic is solved

24 Jan 2024, 15:49

leihu wrote:
24 Jan 2024, 11:39
If you think there's no way to move the cursor to the current view using COM or any other methods, I'll give up.

I just said that it cannot be done fully with COM. The "any other method" is fairly easy. You just simulate a click in the active Word window. You do not have to actually move the mouse to do this. You can simulate a mouse click using ControlClick without actually moving and clicking the mouse.

Code: Select all

F1::
{
	wdApp := ComObjActive("Word.Application")
	wdHwnd := wdApp.ActiveWindow.Hwnd
	Width := wdApp.PointsToPixels(wdApp.ActiveWindow.Width, false)
	Height := wdApp.PointsToPixels(wdApp.ActiveWindow.Height, true)
	ControlClick('X' Width / 2 ' Y' Height / 2, wdHwnd)
}
This just basically goes for the middle of the overall window, but a more sophisticated method could be used if the exact positioning of the cursor is important.

Hitting the down arrow moves the insertion point, moving the insertion point always scrolls the window to show the new insertion point. The only way to not have the window scroll when hitting the down arrow is to have the insertion point already in view.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
leihu
Posts: 51
Joined: 19 Mar 2020, 22:08

Re: How to move Word cursor?

25 Jan 2024, 20:49

ControlClick! Cool! Thanks a lot!

FanaticGuru wrote:
24 Jan 2024, 15:49
leihu wrote:
24 Jan 2024, 11:39
If you think there's no way to move the cursor to the current view using COM or any other methods, I'll give up.

I just said that it cannot be done fully with COM. The "any other method" is fairly easy. You just simulate a click in the active Word window. You do not have to actually move the mouse to do this. You can simulate a mouse click using ControlClick without actually moving and clicking the mouse.

Code: Select all

F1::
{
	wdApp := ComObjActive("Word.Application")
	wdHwnd := wdApp.ActiveWindow.Hwnd
	Width := wdApp.PointsToPixels(wdApp.ActiveWindow.Width, false)
	Height := wdApp.PointsToPixels(wdApp.ActiveWindow.Height, true)
	ControlClick('X' Width / 2 ' Y' Height / 2, wdHwnd)
}
This just basically goes for the middle of the overall window, but a more sophisticated method could be used if the exact positioning of the cursor is important.

Hitting the down arrow moves the insertion point, moving the insertion point always scrolls the window to show the new insertion point. The only way to not have the window scroll when hitting the down arrow is to have the insertion point already in view.

FG

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: kunkel321, shipaddicted, Spikea and 68 guests