View Single Post
 
Old 05-15-2016, 12:15 PM
superxl superxl is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: May 2016
Posts: 1
superxl is on a distinguished road
Default 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
Reply With Quote