Global Text Replacement

S

Steve Cronin

Folks;

What I want to do is to replace ALL occurrences of 'ABC' with 'XYZ'.
When I say ALL I mean ALL.
Occurrences in the mainDocument, Headers, Footers, TextBoxes, TextBoxes
in Headers, TextBoxes in Footers, Tables, Tables in Headers, and
Tables in Footers.

This on a Mac using 'do Visual Basic'. I've posted something in the
Mac forum and it seems as soon as I mention 'do Visual Basic' I get
pointed over here.

Anyway a simple Find/Replace doesn't scour the text of Tables nor
TextBoxes, though I've been told it should? could?

The code below does what I need except for one small thing ...
It doesn't work when I change the ActivePane to wdSeekPrimaryHeader or
wdSeekPrimaryFooter

So I've got it working MainDocument but the code goes brain-dead in
Headers and Footers...
Can someone point out what I need to do?

Also the code below has an odd nested construct. I found similar code
in the MSHelp files.
Can someone clarify why the For .. While Not .. Wend Next construct is
necessary?
The code doesn't work in I unwind it to a simple For Next loop.

_____________________________
ActiveDocument.ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
For Each myStoryRange In ActiveDocument.StoryRanges
myStoryRange.Find.Execute
FindText:="ABC",ReplaceWith:="XYZ",Replace:=wdReplaceAll
While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
myStoryRange.Find.Execute FindText:=
"ABC",ReplaceWith:="XYZ",Replace:=wdReplaceAll
Wend
Next myStoryRange
 
J

Jezebel

The ActiveWindow statement in your code is irrelevant. The Find is working
on the StoryRanges, which are not affected by what's displayed. The problem
is in the Loop structure itself. There are seven possible types of
storyrange. Some of these are necessarily single items (like the Main
story -- you can only ever have one of these); but others are effectively
linked lists. So to get every story range, you need a structure like --

For each myStoryRange in ActiveDocument.StoryRanges

Do
. .. do whatever

set myStoryRange = myStoryRange.NextStoryRange
Loop until myStoryRange is nothing

Next
 
S

Steve Cronin

Jezebel:

The code below does everything EXCEPT textBoxes in Headers and Footers.
It handles tables everywhere and it does handle textBoxes in the
MainDocument.
I am becoming convinced that this may be a limitation of the Mac
implementation of Visual Basic.
If I understand your comments you believe that this should do it all,
yes?

For Each r In ActiveDocument.StoryRanges
Do
r.Find.Execute FindText:=
\"<SOrg>\",ReplaceWith:=\"Mac-Chi\",Replace:=wdReplaceAll
Set r = r.NextStoryRange
Loop until r is nothing
Next
 
P

Peter Jamieson

The limitation is in the Windows version of Wor as well. (see the article I
quote in the other branch of this thread).

Peter Jamieson
 

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