View Full Version : Change Heading1
gentle
11-03-2010, 04:37 PM
Can anyone please help me with the following.
I want a macro that looks for all the text in a document that is in the style of heading1.
it must then convert what is found to Normal style , cambria (Heading), size 16 and then underline it.
: pray2:
Faith
macropod
11-03-2010, 08:32 PM
Hi Faith,
You can do this using Find/Replace, without the need for a macro. Simply expand the 'More' option and, for the Find & Replace expressions, choose the formatting attributes you want.
Having said that, here's a macro:
Sub ChangeHeadingFormats()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = ""
.Style = .Style = "Heading 1"
With .Replacement
.ClearFormatting
.Text = ""
.Style = "Normal (Cambria)"
With .Font
.Size = 16
.Underline = wdUnderlineSingle
End With
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
You might need to change the '.Style = "Normal (Cambria)"' expression, as I'm not sure what you mean by:
convert what is found to Normal style , cambria (Heading)
Perhaps you mean:
.Style = "Normal"
With .Font
.Name = "Cambria"
.Size = 16
.Underline = wdUnderlineSingle
End With
As an aside, you really should define an appropriate Style with the attributes you want, then simply use that as the replacement Style, rather than applying hard-formatting to the newly-replaced Style.
gentle
11-04-2010, 04:02 AM
Hi macropd
Thank you for the help .
I have got it to work.
BUT
I would like it to center the text I choose. I added it here but it does not centre it.
Not sure what I have done wrong.
This Part works
With .Font
.Name = "Cambria"
.Size = 16
.Bold = wdToggle
.UnderlineColor = wdColorAutomatic
.Underline = wdUnderlineSingle
This part does not work
.Alignment = wdAlignParagraphCenter
Here is what I now have.
Sub ChangeHeadingFormats()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = ""
.Style = ActiveDocument.Styles("Heading 1")
With .Replacement
.ClearFormatting
.Text = ""
.Style = "Normal"
With .Font
.Name = "Cambria"
.Size = 16
.Bold = wdToggle
.UnderlineColor = wdColorAutomatic
.Underline = wdUnderlineSingle
.Alignment = wdAlignParagraphCenter
End With
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
'ActiveDocument.Save
End Sub
Thanks
Faith
gentle
11-04-2010, 06:28 AM
Sorry one thing the heading 1 will always be no more than 1 row.
not sure if that will help
fumei
11-04-2010, 09:14 AM
Really, irt would be best,as macropod states, to use Word properly, and that is using Styles. This is how Word is designed to be used.
So make a new Style, say named MyCambria. You can define ALL parameters: font size, alignment, Bold - whatever you want. Now you simply replace the Header 1 style with your own Style.
Sub ChangeHeadingFormats()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = ""
.Style = ActiveDocument.Styles("Heading 1")
With .Replacement
.ClearFormatting
.Text = ""
.Style = "MyCambria"
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
macropod
11-06-2010, 10:56 PM
Hi Faith,
You can't change a paragraph's alignment using a straight-forward Find/Replace, other than by changing the paragraph Style name to a paragraph Style that already has the required alignment applied.
If this really isn't a viable option for you, post back and someone here can show you how it's done.
fumei
11-09-2010, 09:31 AM
Which is all the more of a reason to use a style properly (explicitly).
The reason you can not do a change of alignment is because alignment is a property of the range, which you are NOT actioning with a Find/Replace. As macropod mentions, it can be done. Let us know if you need to do it that way.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.