AutoIT: _WordDocFindReplace in TextBox + Disable Macros

The Default _WordDocFindReplace only works with pretty standard word documents (It’s more the fault of the way VBA’s find function work the word.au3). However thanks to a few VBA samples on the web I’ve managed to come up with some code to get around this issue.

First do your normal find & replace to catch anything in the “normal” layout section. The following code example can then be used to search though each textbox in turn:

1
2
3
4
5
For $j = 1 To $oDoc.Shapes.Count ;For 1 to total number of textboxes
	$oDoc.Shapes($j).Select ;Select Text box $j
	$oDoc.Application.Selection.WholeStory ;Select content of textbox
	$oFind = _WordDocFindReplace($oDoc, "01924", "xxxxxx",2,-1) ;Find in current selection
Next

And yes $oDoc.Application.Selection.WholeStory is real! It selects the content inside the text box. The -1 parameter of the _WordDocFindReplace tells it to search just the current select. Using 0 will cause _WordDocFindReplace to select all the normal content and search that, ignoring the textbox

As for disabling Macro’s (for example if you want to stop any automatic macros from firing when open a document) you can use:

1
2
$AutomationSecurity=$oWordApp.Application.AutomationSecurity ;Save Settings
$oWordApp.Application.AutomationSecurity=3 ;Disable Macros

To restore the settings use

1
$oWordApp.Application.AutomationSecurity=$AutomationSecurity

Hope someone out there finds this useful.

July 22, 2011 · robert · Comments Closed
Tags: , ,  · Posted in: Howtos, Work / Microsoft