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
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.
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.
Actually it is exactly like that.
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.
So that the next person can benefit from your question could you post how you solved your problem.
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:
I hope this would be helpful.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
Great thanks
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks