[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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
0
dwe0608
Asked:
dwe0608
  • 3
  • 2
1 Solution
 
GrahamSkanCommented:
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

0
 
Rgonzo1971Commented:
Hi,

A Word Doc in composed of different StoryRanges:

wdCommentsStory, wdEndnotesStory, wdEvenPagesFooterStory, wdEvenPagesHeaderStory, wdFirstPageFooterStory, wdFirstPageHeaderStory, wdFootnotesStory, wdMainTextStory, wdPrimaryFooterStory, wdPrimaryHeaderStory, and wdTextFrameStory

to loop through them use
  For Each rngStory In ActiveDocument.StoryRanges 
  Next rngStory 

Open in new window



Reference
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

Regards
0
 
dwe0608Author Commented:
Many thanks for the assistance - that solved the problem.

Regards

DWE
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GrahamSkanCommented:
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.
0
 
dwe0608Author Commented:
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
0
 
GrahamSkanCommented:
That's good news. Thanks for letting us know.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now