+ Reply to Thread
Results 1 to 6 of 6

Thread: Find and Replace in the whole document

  1. #1
    Registered User
    Join Date
    04-16-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    29

    Find and Replace in the whole document

    Hi,

    I have to find and replace particular style in a word document through macros.

    It has to perform this operation in body content, text boxes, header, footer and end notes etc., Ideally it has to perform in the whole document.

    Kindly help me on this.

    Thanks,
    Ram

  2. #2
    Registered User
    Join Date
    11-09-2008
    Location
    germany
    Posts
    74

    Re: Find and Replace in the whole document

    Quote Originally Posted by nnram123 View Post
    I have to find and replace particular style in a word document through macros.
    Can you do this Manually with word?

    If yes then record a macro in Word while doing it manually. Then open the Word Visual Basic Editor and you will see the code. This will give you a good start on the code you will need.

    Because you only want to search the body you might just need to add a selection to the start of your code.

    Once you get some code which more or less works, if you have a specific question come back to this thread, show your code and ask your specific question.

    I hope this gets you on the right track.

  3. #3
    Registered User
    Join Date
    04-16-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Find and Replace in the whole document

    Hi darbid,

    Thanks for your reply.

    It is not something like record a macro and see it. Even if you do that it won't work for other sections like Headers, Footers, Endnote etc., and work only for the body of the content.

    Anyway I got the solution. There is something called wdStory method in the Word VBA. In MS word by default there are 11 sections. To execute the find and replace for all the sections you have to keep this method in the outer loop and execute your code into the inner loop.

  4. #4
    Registered User
    Join Date
    11-09-2008
    Location
    germany
    Posts
    74

    Re: Find and Replace in the whole document

    Quote Originally Posted by nnram123 View Post
    It is not something like record a macro and see it.
    Actually it is exactly like that.

    Quote Originally Posted by nnram123 View Post
    Even if you do that it won't work for other sections like Headers, Footers, Endnote etc., and work only for the body of the content.
    Maybe, maybe not. Doing it manually on a section and recording the result will tell you if it can be reproduced or not later. As I expected the resulting macro is not so friendly for copying and pasteing when being done in header of footer. But one can see that what is required is a selection and a simple google search of how to make a selection of a header or footer is much simpler.

    Quote Originally Posted by nnram123 View Post
    Anyway I got the solution.
    So that the next person can benefit from your question could you post how you solved your problem.

  5. #5
    Registered User
    Join Date
    04-16-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Find and Replace in the whole document

    For instance if I want to find and replace a particular style from the word document through VBA code (including body content, Header, Footer, Endnote, Text box). I will create an Add-in file and while adding this add-in file into the user's document, a button will be enabled to execute the following code.

    The following code will do the execution to the whole document:

    Dim rStoryRange As Range
    For Each rStoryRange In ActiveDocument.StoryRanges
    With rStoryRange.Find
    .ClearFormatting
    .Text = ""
    .Wrap = wdFindContinue
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Style = styFind
    .Replacement.ClearFormatting
    .Replacement.Style = styReplace
    '.Replacement.Text = "^&"
    .Execute Replace:=wdReplaceAll
    End With
    Next
    I hope this would be helpful.

  6. #6
    Registered User
    Join Date
    11-09-2008
    Location
    germany
    Posts
    74

    Re: Find and Replace in the whole document

    Great thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0