I’m trying to use the change command provided with Smile and it satimage.osax.
I can get the result I want in the result window, but not in the file per say. here is a simple test I did to see it it would work.
tell application "Microsoft Word"
set theText to text in document 1
change "e" into "u" in theText
end tell
This is with a word document containing the world Hello.
The text in document 1 doesn’t change for Hullo but the result window says it should. So what’s wrong and how can I make this work. This would be for long text document with lots of change and replace. I would also use the RegExp of that command. So this is just a simple test to see how it works first.
tell application “Microsoft Word”
set theText to text in document 1
change “e” into “u” in theText
end tell
sets the variable theText to the text of teh front document. The next statement returns a result of the changed text. As you describe it, it doesn’t change the text in the document. To change the text in the document, you need to script Microsoft Word.
I don’t have the newest version of Word, so can’t help you there if you have that version.
“change” works only internally in your script. So, you must apply later the result of the search/replace operation:
tell application "Microsoft Word" to ¬
tell text object of active document to ¬
set content to (change "e" into "u" in (get content))
Anyway, Word 2004 provides robust tools to search/replace text itself. This is an example:
tell application "Microsoft Word"
execute find (get find object of text object of active document) find text "e" replace with "u" replace replace all
end tell
And you can also record your own VB macros and use them in your script:
tell application "Microsoft Word"
do Visual Basic "Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "e"
.Replacement.Text = "u"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll"
end tell
I know about the VB macros, but I wanted to make cleaner looking script since I have a lot of those find and replace to do. I should also say that I’m on word X and not 2004 for the moment. I would guess that a script that worked in X would work in 2004 but not the other way around since 2004 made some significant improvement in its support of AS.
Do you know if the formating of my text in word will change if I use your first example? I need the formating to stay unchanged.
Most probably you will loss formatting using “change”, since using change you are manipulating a “string”, not a formatted text. I think the “execute find” command is not available in previous versions. I don’t know why you say the VB statements are not “clean” code. They are very visual… And very basic! :rolleyes:
Thanks for clarifying all this! My reason for not finding the VB code “clean” were purely estetical. They are long and kind of clutter the script when you have about 20 of them to do. I’ll try to put that in a handler and see if I can make it work. I’m pretty new at all this.