How to open a doc and find a string

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Emma
Posts: 5
Joined: 13 Feb 2016, 07:37

How to open a doc and find a string

25 Oct 2021, 01:24

Hi, I'm trying to copy a string, open a Word doc and paste the string into the Find box.
Using this code,

Code: Select all

^!a::
	Send, ^c
	Run C:\[path to my doc]\mydoc.docx 
	Send, ^f
	Send, %clipboard%
return
The docx opens correctly, but the "Find" command is opening wherever I come from (e.g., in Chrome), so it seems the Word doc doesn't become the active window.
How can I solve this?
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How to open a doc and find a string

25 Oct 2021, 02:10

Try to use WinWait, WinActivate and WinWaitActive for both the Word and the Find windows before sending commands to them.
I would also empty the clipboard before and add ClipWait after sending ^c.
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How to open a doc and find a string

25 Oct 2021, 05:22

Once you are familiar with the above commands you can also try this:

Code: Select all

^!a::
	ClipSaved := ClipboardAll  ; save the entire clipboard to the variable ClipSaved
	clipboard := ""            ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
	Send, ^c                   ; copy the selected text,
	ClipWait, 1                ; wait for the clipboard to contain data. 
	if (!ErrorLevel)           ; If NOT ErrorLevel clipwait found data on the clipboard
	{
		If !WinExist("mydoc ahk_class OpusApp")  ; title and ahk_class 
		{
			oWord := ComObjCreate("Word.Application") ; create MS Word object
			oWord.Visible := True
			file := "C:\path to my doc\mydoc.docx" ; Path to file
			oWord.Documents.Open(file)
		}
		oWord := ComObjActive("Word.Application") ; activate an open one doc
		oWord.Selection.Find.ClearFormatting
		oWord.Selection.Find.Text := clipboard
		oWord.Selection.Find.Wrap := 1
		oWord.Selection.Find.Execute
	}
	else
		MsgBox, No clipboard data
	clipboard := ClipSaved       ; restore original clipboard
return
Emma
Posts: 5
Joined: 13 Feb 2016, 07:37

Re: How to open a doc and find a string

27 Oct 2021, 02:49

Thank you for your help, GEV. I'm unfamiliar with the commands you mention so I've read up the entries in the help files.

The code below opens my Word file and the search panel, but pastes the clipboard string into the top of the document itself (not in the search box).

Code: Select all

#+t::
clipboard := ""            ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
	Send, ^c                   ; copy the selected text,
	Run WINWORD.exe, , Max
	WinWait, Untitled - WINWORD,, 3
	Run C:\path to my doc\mydoc.docx
	if WinExist("Untitled - WINWORD")
    WinActivate ; Use the window found by WinExist
	Send, ^f
	Send, %clipboard%
	return
How can I get Word to paste the string into the search box instead of the body of the document?
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How to open a doc and find a string

27 Oct 2021, 06:06

Code: Select all

#+t::
	ClipSaved := ClipboardAll  ; save the entire clipboard to the variable ClipSaved
	clipboard := ""            ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
	Send, ^c                   ; copy the selected text
	ClipWait, 1                ; wait for the clipboard to contain data. 
	if (!ErrorLevel)           ; If NOT ErrorLevel clipwait found data on the clipboard
	{
		If !WinExist("title of mydoc.docx")  ; Use Window Spy to get the exact title
			Run C:\[path to my doc]\mydoc.docx, , Max
		WinWait, title of mydoc.docx ; Use Window Spy to get the exact title
		WinActivate title of mydoc.docx ; same here
		WinWaitActive, title of mydoc.docx ; and here
		Send, ^f
		Sleep, 300 ; wait for the search box
		Send, ^v  ; paste the clipboard content in the search box
	}
	else
		MsgBox, No clipboard data
	clipboard := ClipSaved       ; restore original clipboard
return
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How to open a doc and find a string

27 Oct 2021, 10:52

Emma wrote:
27 Oct 2021, 02:49
I'm unfamiliar with the commands you mention so ...
Before you started with AHK you were unfamiliar with all commands...
@GEV has it right. In its simplest form the COM code for finding the word "test" would be:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.Selection.Find.Text := "test"
oWord.Selection.Find.Execute
Significantly better than using simulated keystrokes!
14.3 & 1.3.7
Emma
Posts: 5
Joined: 13 Feb 2016, 07:37

Re: How to open a doc and find a string

27 Oct 2021, 11:31

That works perfectly! Thank you so much, GEV!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, mikeyww, scriptor2016 and 273 guests