Using COM to manipulate Microsoft Word's Navigation task pane

Get help with using AutoHotkey and its commands and hotkeys
iPhilip
Posts: 299
Joined: 02 Oct 2013, 12:21

Using COM to manipulate Microsoft Word's Navigation task pane

11 Aug 2017, 18:18

Hi folks,

I am looking for a COM solution that's equivalent to using the keyboard shortcut ^f for "Find" in Microsoft Word. Basically, I am trying to find an alternative to the following simple command:

Code: Select all

Send, ^f
The following code opens a "Find and Replace" window:

Code: Select all

oWord := ComObjActive("Word.Application") oWord.Activate oWord.Dialogs(112).Show ; wdDialogEditFind = 112
but what I am really after is opening the Navigation task pane. The lines below do so and bring focus to the edit control, like ^f does:

Code: Select all

oWord := ComObjActive("Word.Application") oWord.Activate oWord.TaskPanes(18).Visible := true ; wdTaskPaneNav = 18 ;~ oWord.CommandBars("Navigation").Visible := true ; this also works ControlFocus, RICHEDIT60W1, A ; Set input focus to the edit control
Is there a COM solution to the ControlFocus command? Also, is there a COM (or non-COM) way to select the Results control that's part of the Headings/Pages/Results controls under the edit box?

Thank you.

- iPhilip

P.S.: The above code was tested under Word 2016.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
iPhilip
Posts: 299
Joined: 02 Oct 2013, 12:21

Re: Using COM to manipulate Microsoft Word's Navigation task pane

16 Aug 2017, 11:00

Bump.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
Elgin
Posts: 106
Joined: 30 Sep 2013, 09:19

Re: Using COM to manipulate Microsoft Word's Navigation task pane

16 Aug 2017, 13:19

I don't know if there's anything in the Word Com object but at least MSAA should be able to get to those controls. Word is usually very well accessible.
See here:
https://autohotkey.com/boards/viewtopic.php?t=26201
and many posts on the forum about using it.
iPhilip
Posts: 299
Joined: 02 Oct 2013, 12:21

Re: Using COM to manipulate Microsoft Word's Navigation task pane

17 Aug 2017, 11:04

Thank you, Elgin, for your recommendation. I spent some time learning about Acc.ahk from those links and came up with the following:

Code: Select all

; References: ; ; https://raw.githubusercontent.com/Drugoy/Autohotkey-scripts-.ahk/master/Libraries/Acc.ahk ; https://raw.githubusercontent.com/Drugoy/Autohotkey-scripts-.ahk/master/DevTools/AccViewer.ahk #Include Acc.ahk #IfWinActive ahk_exe WINWORD.EXE F2:: if !OpenWordFind() MsgBox, 16, , %ErrorLevel% Return #IfWinActive OpenWordFind(Tab:="Results") { static Navigation := {Headings:1,Pages:2,Results:3} Tab := Navigation.HasKey(Tab) ? Tab : "Results" ; Error checking oWord := ComObjActive("Word.Application") ; Get the object associated with the active window oWord.CommandBars("Navigation").Visible := true, oWord := "" ; Make the Navigation task pane visible ControlGet, ControlHwnd, Hwnd,, NetUIHWND1, A ; Get the handle of the control that contains the tabs ; Use Acc to get the coordinates of the corresponding tab. The path "4.6.1.x" was obtained using AccViewer. if !(Pos := Acc_Get("Location", "4.6.1." Navigation[Tab], 0, "ahk_id " ControlHwnd)) Return false RegExMatch(Pos, "i)x(?P<X>\d+).+y(?P<Y>\d+)", p) ; Save the x and y coordinates in pX and pY CoordMode, Mouse, Screen ; Use screen coordinates MouseGetPos, X, Y ; Save the current mouse position MouseMove, pX, pY, 0 ; Move the mouse to the coordinates of the tab Click ; Select the tab by clicking it MouseMove, X, Y, 0 ; Move the mouse to the original position ControlFocus, RICHEDIT60W1, A ; Bring focus to the input control Return true }
The best I could do was get the coordinates of the tab and click it as I didn't see an easy way to use Acc to select it. If you are aware of a simpler method, I would appreciate it if you could let me know.

Cheers!
Last edited by iPhilip on 17 Aug 2017, 11:50, edited 1 time in total.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jeeswg
Posts: 5260
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using COM to manipulate Microsoft Word's Navigation task pane

17 Aug 2017, 11:35

Does this work for you?

Code: Select all

oAcc := Acc_Get("Object", "4.6.1." Navigation[Tab], 0, "ahk_id " ControlHwnd) oAcc.accDoDefaultAction(0) oAcc := ""
Haha I didn't know you could use Acc_Get like that: 'Acc_Get("Location"'.

[EDIT:] I see it has these suggested: Location,Object,Role,State,ChildCount,Selection,Focus, and possibly others will work too.
homepage | tutorials
[code boxes are currently not working 100%]
[click the 'Reply with quote' button on a post to see the full indented text]
iPhilip
Posts: 299
Joined: 02 Oct 2013, 12:21

Re: Using COM to manipulate Microsoft Word's Navigation task pane

17 Aug 2017, 11:54

Yes! Thank you for the suggestion. :) Here is the updated script:

Code: Select all

; References: ; https://raw.githubusercontent.com/Drugoy/Autohotkey-scripts-.ahk/master/Libraries/Acc.ahk ; https://raw.githubusercontent.com/Drugoy/Autohotkey-scripts-.ahk/master/DevTools/AccViewer.ahk #Include Acc.ahk #IfWinActive ahk_exe WINWORD.EXE F2:: if !OpenWordFind() MsgBox, 16, , %ErrorLevel% Return #IfWinActive OpenWordFind(Tab:="Results") { static Navigation := {Headings:1,Pages:2,Results:3} Tab := Navigation.HasKey(Tab) ? Tab : "Results" ; Error checking oWord := ComObjActive("Word.Application") ; Get the object associated with the active window oWord.CommandBars("Navigation").Visible := true, oWord := "" ; Make the Navigation task pane visible ControlFocus, RICHEDIT60W1, A ; Bring focus to the edit control in the Navigation task pane ControlGet, ControlHwnd, Hwnd,, NetUIHWND1, A ; Get the handle of the control that contains the tabs ; Use Acc to get the object of the corresponding tab. The path "4.6.1.x" was obtained using AccViewer. if IsObject(oAcc := Acc_Get("Object", "4.6.1." Navigation[Tab], 0, "ahk_id " ControlHwnd)) oAcc.accDoDefaultAction(0), oAcc := "" ; Select the tab Return IsObject(oAcc) }
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Ask For Help”

Who is online

Users browsing this forum: bitx0r, roysubs, Spawnova and 45 guests