Solved

VB6 and MsWord

Posted on 2014-03-20
6
279 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 49

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now