0
votes

I'm getting a runtime error 91 on the following Word VBA:

Dim myStoryRange As Object
    For Each myStoryRange In ActiveDocument.StoryRanges
    With myStoryRange.find
        .Text = "test to search"
        .Replacement.Text = "text to replace"
        .Wrap = wdFindContinue
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = False
        .Execute replace:=wdReplaceAll
    End With
Next myStoryRange

The error occurs inconsistently on about half of the computers that run the macro.

Thoughts?

Per PatricK's suggestion I've changed the code to read:

Dim myStoryRange As Range
For xStories = 1 To ActiveDocument.StoryRanges.Count

    Set myStoryRange = ActiveDocument.StoryRanges.Item(xStories)
    With myStoryRange.find
        .Text = "[Client Name]"
        .Replacement.Text = Client
        .Wrap = wdFindContinue
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = False
        .Execute replace:=wdReplaceAll
    End With

Next xStories

This seems to fix the Error 91. However, I'm still getting an odd result. This code fails at line #5 (with myStoryRange.find) with the error "The Requested Member of the Collection does not exist" on the second item in the collection.

It fails when there IS a member of the collection. In other words, there are 7 xStories, it fails on xStories = 2. And xStories = 2 is a complete item with all of the properties referenced present.

As an FYI, I'm trying to replace a bit of text in the header of the document. I'm getting a failure on a StoryRange item that is in the header, rather than the body of the document. Can that be the problem?

1
my thoughts ... you have not included all the information ... like, which line gets the error?jsotola
Shouldn't you Dim myStoryRange As Range? You may have to use For loop with ActiveDocument.StoryRanges.Count and Set myStoryRange = ActiveDocument.StoryRanges.Item(#)PatricK
If you're trying to replace text in the header, why not pick the range you want with something like StoryRanges(wdPrimaryHeaderStory) instead of looping through all the stories?xidgel

1 Answers

0
votes

I was able to solve this question (with help from commentors). The answer is that the header text is treated as an item in the Sections collection. This seems inconsistent because it does show up as well as an item in StoryRanges. However I get an error when finding and replacing using that collection.

The following code correctly searches and replaces text in a Word header:

Dim myStoryRange As Range

    For xStories = 1 To ActiveDocument.Sections(1).Headers.Count

        Set myStoryRange = 
        ActiveDocument.Sections(1).Headers.Item(xStories).Range

        With myStoryRange.find
            .Text = "[Client Name]"
            .Replacement.Text = Client
            .Wrap = wdFindContinue
            .ClearFormatting
            .Replacement.ClearFormatting
            .Replacement.Highlight = False
            .Execute replace:=wdReplaceAll
        End With

     Next xStories