Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2016, 12:15 PM
superxl superxl is offline Word macro to change style between tags Windows 7 64bit Word macro to change style between tags Office 2010 64bit
Novice
Word macro to change style between tags
 
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
  #2  
Old 05-15-2016, 03:17 PM
macropod's Avatar
macropod macropod is offline Word macro to change style between tags Windows 7 64bit Word macro to change style between tags Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word macro to change style between tags Tab character causes style change to Heading 4 after macro Jennifer Murphy Word VBA 2 12-14-2015 02:31 AM
Word macro to change style between tags 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
Word macro to change style between tags Macro to change style name with " in it gundas Word VBA 3 04-02-2013 06:20 AM
Word macro to change style between tags How to globally change the formatting of a bullet style to another style? ravl13 Word 5 03-10-2013 05:04 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:38 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft