Ger Cas
Ger Cas

Reputation: 2308

How to change style of some texts to style of Heading 1 without affect rest of text

I want to change the format of some text that have font size = 20 and font name = "Times New Roman" to style of "Heading 1"

The issue is, for example the text is centered and doing manually when Heading 1 is selected the text is sent to left and changed its font size, font name and font color to those that Heading 1. Then, once the text is related with "Heading 1" I can centered again and set the same format options as before and the text remains related with "Heading 1"

I tried to make this with VBA in order to applyt the same as above to all text with size 20 and font "Times New Roman", but my code below it seems to affect the other text within document even that text has different font size.

How can I fix this problem?

Sub ChangeToHeading1()
Dim wdDoc As Document

Set wdDoc = ActiveDocument

    With wdDoc.Range
        .Find.ClearFormatting
        .Find.Font.Size = 20
        .Find.Replacement.ClearFormatting
        .Find.Text = ""
        .Find.Replacement.Text = ""
        .Find.Replacement.Style = ActiveDocument.Styles("Heading 1")
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Font.Name = "Times New Roman"
        .Font.Size = 20         
        .Find.Forward = True
        .Find.Wrap = wdFindContinue
        .Find.Format = True
        .Find.MatchCase = False
        .Find.MatchWholeWord = False
        .Find.MatchWildcards = False
        .Find.MatchSoundsLike = False
        .Find.MatchAllWordForms = False
        .Find.Execute Replace:=wdReplaceAll
    End With
End Sub

Thanks in advance.

Upvotes: 0

Views: 2908

Answers (2)

macropod
macropod

Reputation: 13515

Since you haven't bothered to tell us what colour you want the Heading 1 Style to be, we can only guess. You really ought also spend a bit of time learning how to use VBA, not just the macro recorder - there are also innumerable examples of the kinds of code you need all over the web. For example:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
  With .Styles(wdStyleHeading1)
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Name = "Times New Roman"
    .Font.Size = 20
    .Font.ColorIndex = wdGreen
  End With
  With .Range.Find
    .ClearFormatting
    .Text = ""
    .Font.Size = 20
    .Font.Name = "Times New Roman"
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .Replacement.Style = wdStyleHeading1
    .Format = True
    .Forward = True
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub

Upvotes: 2

Timothy Rylatt
Timothy Rylatt

Reputation: 7860

If I understand correctly you want Heading 1 to be in 20 point Times New Roman and centered. So just modify the style to have the settings you want before you apply it in your find.

Sub ModifyHeading1()
  With ActiveDocument.Styles(wdStyleHeading1)
    With .Font
      .Name = "Times New Roman"
      .Size = 20
    End With
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
  End With
End Sub

Sub ChangeToHeading1()
Dim wdDoc As Document

Set wdDoc = ActiveDocument

    With wdDoc.Range.Find
        .ClearFormatting
        .Font.Size = 20
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Replacement.Style = ActiveDocument.Styles("Heading 1")
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Upvotes: 1

Related Questions