P
pereljon
It does seem that the scripted (AS and VBA) find and replace works
differently that the interactive version.
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
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