Re: Help with find and replace macro



Hi Tony

Thanks for your help. I am glad to know it is supposed to be working! Here
is where I am at now:

Windows XP
Office 2007 Pro

I have created a brand new document. Office button - new - blank document
I have inserted the word FILENAME in the main text, header and footer.
I have cut and pasted the below simplified code for sub Test into the Visual
Basic editor for this document
I then run the code by pressing the run sub key in the visual basic editor
The main text and the header are replaced. The footer is not replaced.

I then added a msgbox statement to display myStoryRange,
myStoryRange.StoryType and myStoryRange.StoryLength just before the With
statement to confirm that it is cycling through each of the story types.

It confirms that it is going through 7 different story types in the
following order.

Story Type Contains Length
wdMainTextStory FILENAME 9
wdPrimaryHeaderStory FILENAME 9
wdPrimaryFooterStory FILENAME 9
wdFootnoteSeparatorStory odd character 2
wdFootnoteContinuationSeparatorStory odd character 2
wdEndnoteSeparatorStory odd character 2
wdEndnoteContinuationSeparatorStory odd character 2

I then used the debug menu and stepped through the macro line by line. It
worked perfectly and replaced all three areas.

I then used the debug menu and placed the cursor at the next MyStoryRange
line and had it run to cursor. I did this seven times for each of the story
types. It worked perfectly.

I then used the debug menu and placed the cursor at the end sub line and had
it run to cursor. Same problem with running the sub. The footnote is not
being replaced.

I also added a line myStoryRange.select immediately before the With
statement and it worked correctly.

I am completely confused. Any advice will be much appreciated.

If it is important, the references available for this project are:
visual basic for applications
microsoft word 12.0 object library
OLE Automation
Normal
Microsoft Office 12.0 Object library

Thanks,

Bryan




"Tony Jollans" wrote:

Well done for investigating! However the code you have posted here works for
me in Word 2007. I just did a very simple test - new document, typed
FILENAME into the three places, cut and pasted and ran your code. Could
there be something unusual about your test document? Are there any more
details you could provide?

--
Enjoy,
Tony

"BigBry" <BigBry@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1C5401A2-FC6E-4E0F-B602-B0A5014ADC6C@xxxxxxxxxxxxxxxx
I have been puzzling over this all day. I have tried to simplify it and
see
if I can figure out what the problem is. I have created a new document
with
a single header and footer. The main text, header and footer have the
word
FILENAME in them. I have simplified the macro to

Sub Test()

Dim myStoryRange As Range

For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "FILENAME"
.Replacement.Text = "John Doe"
.MatchCase = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange

End Sub


When i run this, only the FILENAME in the main text portion is replaced.
The FILENAME in the header and footer are not replaced. I have used
msgbox
to verify that myStoryRange is including the header and footer and it is.

Any suggestions?

Thanks,

Bryan


.