Solved

VB6 and MsWord

Posted on 2014-03-20
6
276 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 48

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now