Word 2004 and AppleScript

P

pereljon

It does seem that the scripted (AS and VBA) find and replace works
differently that the interactive version.
When the Find or Replace utility on the Edit menu is used, it will find or replace text no
matter where it appears in the document. If you record that action however, it will only act
on the text in the body of the document and it will have no effect on text that is in the headers
or footers of the document, for example, or in a textbox, footnotes, or any other area that is
outside the main body of the document.

from from http://word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
where I originally got my VBA code....

Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "findme"
.Replacement.Text = ""
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With Next myStoryRange

Unfortunately, my VBA is not so great and I'd like to not do a find and
replace on story sections that are not empty.... I'm trying to run
several find/replace actions across many files, some of which may have
a primary header or footer....

Which leads me to converting that VBA to AppleScript and trying to not
run the replace if the story is empty/does not exist...

here's a script I did to test that I'm accessing the story ranges
correctly... seems to work...
tell application "Microsoft Word"
activate
set theText to ""
set storyTypes to {main text story, footnotes story, endnotes story,
comments story, text frame story, even pages header story, primary
header story, even pages footer story, primary footer story, first page
header story, first page footer story}
repeat with theType in storyTypes
try
set myRange to get story range active document story type theType
set theText to "The " & theType & " story is available." & return &
theText
on error
set theText to "The " & theType & " story is NOT available." &
return & theText
end try
end repeat
display dialog theText
end tell

- BTW "even pages footer story" is wrong in the manual which lists as
"even pages header footer story"

oh... again the ideal would be using the "set all documents to true",
which seems to work once in a while (and even replaces across all
story ranges) and crash Word most of the time...
-
This is the code I'm trying to get to work... I need the try block to
run the find only in valid stories... i'm debugging with the "theText"
debug points... I get to the "a" debug point but not to the "b" debug
point... sorry for the long post... I've been working on this for a
while and I'm at my wits end... Not sure whether to try learning VBA
better to implement in VBA at this point... this is being called from
applescript, so it'd be cleaner to make it all applescript....

tell application "Microsoft Word"
activate
set theText to ""
set storyTypes to {main text story, footnotes story, endnotes story,
comments story, text frame story, even pages header story, primary
header story, even pages footer story, primary footer story, first page
header story, first page footer story}
repeat with theType in storyTypes
try
set myRange to get story range active document story type theType
set theText to "The " & theType & " story is available." & return &
theText
set findSet to find object of myRange
set theText to "a" & return & theText
clear formatting of findSet
set theText to "b" & return & theText
set content of findSet to "a"
set theText to "c" & return & theText
set replSet to replacement of findSet
set theText to "d" & return & theText
tell replSet
clear formatting
set content to "*@*"
end tell
execute find replace replace all
set theText to "The " & theType & " done." & return & theText

on error
set theText to "The " & theType & " story is NOT available." &
return & theText
end try
end repeat
display dialog theText
end tell
 
P

Paul Berkowitz

This is the code I'm trying to get to work... I need the try block to
run the find only in valid stories... i'm debugging with the "theText"
debug points... I get to the "a" debug point but not to the "b" debug
point... sorry for the long post... I've been working on this for a
while and I'm at my wits end... Not sure whether to try learning VBA
better to implement in VBA at this point... this is being called from
applescript, so it'd be cleaner to make it all applescript....

tell application "Microsoft Word"
activate
set theText to ""
set storyTypes to {main text story, footnotes story, endnotes story,
comments story, text frame story, even pages header story, primary
header story, even pages footer story, primary footer story, first page
header story, first page footer story}
repeat with theType in storyTypes
try
set myRange to get story range active document story type theType
set theText to "The " & theType & " story is available." & return &
theText
set findSet to find object of myRange
set theText to "a" & return & theText
clear formatting of findSet
set theText to "b" & return & theText
set content of findSet to "a"
set theText to "c" & return & theText
set replSet to replacement of findSet
set theText to "d" & return & theText
tell replSet
clear formatting
set content to "*@*"
end tell
execute find replace replace all
set theText to "The " & theType & " done." & return & theText

on error
set theText to "The " & theType & " story is NOT available." &
return & theText
end try
end repeat
display dialog theText
end tell



You've used

clear formatting of findSet

as if clear formatting were a property of find. But it isn't: it's a command
that takes the find object as its direct object. So it's erroring on every
loop. You need

clear formatting findSet

instead. (You get it right lower down for replSet).



HOWEVER, the main problem is that

get story range active document story type anyType

produces a range whose find object is always

missing value

This would be a bug. Every text range ought to have a find object. Because
it's missing value no further commands work on it.


find object of (text object of active document)
--> find id 194595928 of text object of active document of application
"Microsoft Word"


whereas

find object of (get story range (active document) story type main text
story)
--> missing value

even though

get story range (active document) story type main text story
--> text range 1 of application "Microsoft Word"

Supposedly that's a text range, but note that it is not referenced by id. I
think it's bogus. I'll report it as a bug.


get story range (active document) story type main text story
properties of result
--> {class:text range, content:missing value, formatted text:missing
value, start of content:missing value, end of content:missing value, font
object:missing value, story type:missing value, footnote options:missing
value, endnote options:missing value, show Word comments by:missing value,
border options:missing value, shading:missing value, text retrieval
mode:missing value, paragraph format:missing value, list format:missing
value, show hidden bookmarks:missing value, bold:missing value,
italic:missing value, underline:missing value, emphasis mark:missing value,
disable character space grid:missing value, style:missing value, story
length:missing value, language ID:missing value, subdocuments
expanded:missing value, grammar checked:missing value, spelling
checked:missing value, highlight color index:missing value, column
options:missing value, row options:missing value, is end of row mark:missing
value, bookmark id:missing value, previous bookmark id:missing value, find
object:missing value, page setup:missing value, case:missing value,
orientation:missing value, next story range:missing value, language ID east
asian:missing value, supplemental language ID:missing value, fit text
width:missing value, no proofing:missing value}


Every single property is 'missing value' - a pretty useless range.

You'd better do it in VBA or 'do Visual Basic'.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
P

pereljon

Thanks for the breakdown Paul!
I tried many alternate versions of the code and aways received errors
and missing values... my guess was also <GASP!> a bug in a Microsoft
product...

The real pitty is that you are not able to access the same
functionality (replace across all documents and all sections) in
AppleScript (or even in VBA for that matter) as you can in the user
interface... Too bad. The ALL DOCUMENTS setting is an awsome way to
make a huge number of replacements across many documents. :-(

Thanks again paul!

JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top