Microsoft Word Active Document GoTo some text...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Architect123
Posts: 2
Joined: 27 Sep 2018, 17:20

Microsoft Word Active Document GoTo some text...

27 Sep 2018, 17:29

I apologize, this should be simple. I don't expect it to be more than 4-5 lines.

I need a script for Microsoft Word. When F1 is pressed, then the document moves to the table of contents. When F2 is pressed, the document searches for "Hello World" and moves to it. Basically, I am positioning to common locations inside an active word document.

I have been working at this for a while. I finally give up and need to ask the community...

F1::
oWord := ComObjActive("Word.Application")
oRange := oWord.ActiveDocument.range().find
oRange.clearformatting()
oRange.Wrap:=1
oRange.Execute("Table Of Contents")
Return

F2::
oWord := ComObjActive("Word.Application")
oRange := oWord.ActiveDocument.range().find
oRange.clearformatting()
oRange.Wrap:=1
oRange.Execute("Hello World")
Return
Architect123
Posts: 2
Joined: 27 Sep 2018, 17:20

Re: Microsoft Word Active Document GoTo some text...

27 Sep 2018, 22:01

I seem to have found a method that works:

oWord := ComObjActive("Word.Application")
oWord.ActiveWindow.Selection.Find.ClearFormatting
oWord.ActiveWindow.Selection.Find.Text := "TOC"
oWord.ActiveWindow.Selection.Find.Wrap := 1 ; wdFindContinue
oWord.ActiveWindow.Selection.Find.Execute("Table Of Contents")
Return
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Microsoft Word Active Document GoTo some text...

28 Sep 2018, 15:47

Architect123 wrote:I seem to have found a method that works:

oWord := ComObjActive("Word.Application")
oWord.ActiveWindow.Selection.Find.ClearFormatting
oWord.ActiveWindow.Selection.Find.Text := "TOC"
oWord.ActiveWindow.Selection.Find.Wrap := 1 ; wdFindContinue
oWord.ActiveWindow.Selection.Find.Execute("Table Of Contents")
Return
It can be done in one line like this:

Code: Select all

ComObjActive("Word.Application").Selection.Find.Execute("Table Of Contents",,,,,,,1)
That will find "Table Of Contents" in the document. The "1" is in the Wrap parameter slot and causes it to wrap back to the beginning if nothing found below the current position.

If you want to do it more in line with your code, there is some redundancy.

Code: Select all

	oWord := ComObjActive("Word.Application")
	oWord.Selection.Find.ClearFormatting
	oWord.Selection.Find.Text := "Table Of Contents"
	oWord.Selection.Find.Wrap := 1 ; wdFindContinue
	oWord.Selection.Find.Execute
This method defines the parameters separately then Executes the find.

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, just me and 223 guests