becks
becks

Reputation: 2718

Can I open a word document and execute find operation using a script?

Can I do this operation in any command line or any scripting language ?

I have a string and I want to search for it in word document exactly as a user would do it.

Steps:

1- I want to open the document.

2- select the text that matches the string I have.

Upvotes: 0

Views: 1748

Answers (2)

Fidel
Fidel

Reputation: 7397

Yep, easy in autoit. This will search & highlight the word "three":

#include <Word.au3>

$oWordApp = _WordCreate(@ScriptDir & "\blah.doc")
$oDoc = _WordDocGetCollection($oWordApp, 0)

$FindText = "three"
With $oWordApp.Selection.Find
    .Forward = True
    .ClearFormatting
    .Wrap = 1 ;.Wrap could be $wdFindContinue which is 1
    .Execute($FindText)
EndWith

Upvotes: 0

Mel Kicchi
Mel Kicchi

Reputation: 188

You can do this with AutoHotkey or the closed-source but free AutoIt. Both of them can emulate user input and interact with the operating system through special scripting languages.

If you use one of the recent versions of Microsoft Word, this page could help you. It features a short example of how to control Microsoft Word's main interface with AutoHotkey. You'd just have to add some code to press Ctrl-F, input your search string and press the find button.

Alternatively, Ekkehard's links could also be useful if you prefer to directly use Word's macro functionality.

Upvotes: 1

Related Questions