Solved

VB6 and MsWord

Posted on 2014-03-20
6
283 Views
Last Modified: 2014-03-22
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
Comment
Question by:dwe0608
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39944563
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
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39944564
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
 
LVL 1

Author Closing Comment

by:dwe0608
ID: 39944619
Many thanks for the assistance - that solved the problem.

Regards

DWE
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39944661
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
 
LVL 1

Author Comment

by:dwe0608
ID: 39946879
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39947158
That's good news. Thanks for letting us know.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question