Link to home
Start Free TrialLog in
Avatar of dwe0608
dwe0608Flag for Australia

asked on

VB6 and MsWord

Hi Guys,

Trying to use the find and replace function from VB6 with MSWord - coding as follows:

    'On Error Resume Next
    With wb.Document.Application.Selection.Find
        
        .ClearFormatting
        .Forward = True
        .Wrap = Word.WdFindWrap.wdFindContinue
        
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        
        .Text = strFind
        
        With .Replacement
            .ClearFormatting
            .Text = sReplace
        End With
        
        If iMode = SingleReplace Then
           .Execute Replace:=Word.WdReplace.wdReplaceOne
        Else
           .Execute Replace:=Word.WdReplace.wdReplaceAll
           
        End If
        
        
    End With

Open in new window


The code works fine for words in the text body but doesn't recognise any of the words which match the string to find in either a header or a footer.

Can someone enlighten me on why this would be the case?

MTIA


DWE
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

By default, only the Main story is searched. You can step through all the stories like this:

Sub AllStories()
    Dim rngStoryType As Word.Range
    Dim rngCurrentStory As Word.Range
      ' Go through all story ranges in the document, including shapes, headers, footers, text boxes footnotes
    ' headers & footers.
    For Each rngStoryType In ActiveDocument.StoryRanges
        Set rngCurrentStory = rngStoryType 'set rngCurrentStory to first range in story
        Do
            FindAndReplaceInRange rngCurrentStory
            Set rngCurrentStory = rngCurrentStory.NextStoryRange
        Loop Until rngCurrentStory Is Nothing
    Next rngStoryType
End Sub

Sub FindAndReplaceInRange(rng As Word.Range)
    With rng.Find
        .Text = strFind
        
        With .Replacement
            .ClearFormatting
            .Text = sReplace
        End With
        
        If iMode = SingleReplace Then
           .Execute Replace:=Word.WdReplace.wdReplaceOne
        Else
           .Execute Replace:=Word.WdReplace.wdReplaceAll
           
        End If
        
        
    End With
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dwe0608

ASKER

Many thanks for the assistance - that solved the problem.

Regards

DWE
Note that simply stepping through the StoryRanges collection only returns the the range of the first instance of each story type. This might have worked with your current document, but it could miss out parts of a more complex document. If you encounter this in future, bear in mind the code in my comment above.
Avatar of dwe0608

ASKER

Hi GrahamSkan, at the time of accepting the answer, your answer was not visible - I dont know how that happened ... had I seen your answer I probably would have accepted it .... the implementation I utlised is not too dissimilar to what you have posted.

Many thanks for the input ....

DWE
That's good news. Thanks for letting us know.