Hi,
I’m trying to perform a search in all open documents. My code below returns an error from Word that “connection is invalid”. Any hints on how I can enable this search/replace to work correctly? Thanks.
tell application "Microsoft Word"
activate
set WordFindObject to find object of text object of active document
set WordFindObjectProps to {all documents:true}
set properties of WordFindObject to WordFindObjectProps
execute find WordFindObject find text "-" replace with "^~" replace replace all
end tell
ted:
The error seems to lie somewhere in the setting of the properties of the find/replace to {all documents:true}. I attempted a number of approaches with that and always received the same error message. So, I just had Word loop through all the open documents like this:
tell application "Microsoft Word"
set cd to count documents
repeat with a from 1 to cd
set myFind to find object of text object of document a
set content of myFind to "-"
clear formatting myFind
set content of replacement of myFind to "^~" --I got this to work with a string ("dash")
clear formatting replacement of myFind
execute find myFind replace replace all
end repeat
end tell
It worked just fine when I had a string in the [content of replacement] quotes, but when I put your desired replacement item in, I see nothing has happened to the document. I also tried removing the [clear formatting] command of the replacement content, but still no joy. Are you actually trying to replace formatting as well as text with this?
Have you downloaded the humongus pdf on using AppleScript with Word 2004 from Microsoft’s site? It is lengthy, and only somewhat helpful, but it may point you in the right direction.
casdvm
Thanks so much, casdvm, for confirming my suspicion that Word 2004 for the Mac is not as AppleScriptable as we would hope.
I also resorted to looping through the individual documents, with success. It would be very nice if we could use AppleScript to find/replace through all open documents at once, though! (Since AppleScript is the only scripting currently language I know.)
Great recommendation about clearing the formatting for each execute find command.
Now I’m trying to figure out why the following Word regular expression places a non-breaking space after the first word of a paragraph, when it should place the non-breaking space before the last word!
" (<*>.)" this is the find expression
“^s\1.” this is the replace expression
Thanks again and good day!
Ted
Ted:
Yeah, Word is a great word processor, but difficult to script, other than for basic things. Here is the link to the information provided by Microsoft:
http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice
Good luck.
casdvm