#1
|
|||
|
|||
Word macro to change style between tags
I am somewhat of a macro newbie. I have a word document where I will put in tags for what will become the headings in the document. As such, I would like to make a macro where anything between <h3> and </h3> is given a style of Heading3 and a font size of 12. The macro would go though the whole word file doing this. Below is a macro I am using that kind of works, but in some instances, it also applies the heading style to text after the heading. The problem has to do with wrapping - some headings exceed one line and somehow this code does not properly select wrapped text. For example, if I change the page layout to landscape, the below macro works properly. Any advice would be greatly appreciated.
Sub HEADING3_REPEAT() Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting With Selection.Find .Text = "<h3>" .Replacement.Text = "" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = True .MatchWholeWord = False .MatchByte = False .CorrectHangulEndings = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False .MatchFuzzy = False End With Selection.Find.Execute Do While Selection.Find.Found Selection.MoveEnd Unit:=wdLine If Selection.Characters.Last = vbCr Then Selection.MoveEnd Unit:=wdCharacter, Count:=-1 End If Selection.StartOf Unit:=wdParagraph Selection.MoveEnd Unit:=wdParagraph Selection.EndKey Unit:=wdLine, Extend:=wdExtend Selection.Style = ActiveDocument.Styles("Heading 3") Selection.Font.Size = 12 ' I added this Selection.EndKey Unit:=wdLine Selection.MoveDown Unit:=wdLine, Count:=1 Selection.Find.Execute Loop Selection.HomeKey Unit:=wdStory End Sub |
#2
|
||||
|
||||
Heading Styles are paragraph Styles, so applying them to paragraphs containing your nominated text means the whole paragraph gets formatted that way; it's unavoidable. As for changing the font, you really should be doing that by changing the Style definition. Furthermore, you don't even need a macro for this - a simple Find/Replace would do.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Tab character causes style change to Heading 4 after macro | Jennifer Murphy | Word VBA | 2 | 12-14-2015 02:31 AM |
Style affecting other paragraphs around the word I'm trying to change. | Bahamut5098 | Word | 2 | 03-02-2015 01:42 PM |
Find instance of a word in a specific style and change its color | hwg | Word VBA | 7 | 02-20-2014 10:59 PM |
Macro to change style name with " in it | gundas | Word VBA | 3 | 04-02-2013 06:20 AM |
How to globally change the formatting of a bullet style to another style? | ravl13 | Word | 5 | 03-10-2013 05:04 PM |