• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Apply built-in style heading 1 to paragraphs where the word "Speaker" occurs

Dear Experts:

I would like to have a macro that performs the following task:

As soon as the macro hits the word "Speaker", built-in style heading 1 is to applied to the paragraph where the word "Speaker" occurs.

Help is much appreciated.

Thank you very much in advance.

Regards, Andreas
Andreas Hermle
Andreas Hermle
2 Solutions
You can create this with a Find / Replace. Turn your macro recorder on. Find "Speaker", Expand your Replace dialog box, so you can see the whole thing. (More) click the Format button, and then choose Style and select Heading 1. I would not put the word Speaker in the Replace box.

Hopefully that helps.

Dawn Bleuel
Word MVP
Chris BottomleySoftware Quality Lead EngineerCommented:
Try the following:

Sub speaker()
Dim para As Long

    On Error GoTo 0
    For para = 1 To ThisDocument.Paragraphs.Count
        If InStr(1, ThisDocument.Paragraphs(para).Range.Text, "speaker", vbTextCompare) > 0 Then
            ThisDocument.Paragraphs(para).Range.Style = wdStyleHeading1
        End If
End Sub

Open in new window


Sub Macro1()
' Macro1 Macro
' Macro recorded 29-4-2010 by mawa
  Selection.Find.Replacement.Style = ActiveDocument.Styles(wdStyleHeading1)
  With Selection.Find.Replacement.ParagraphFormat
    .SpaceBeforeAuto = False
    .SpaceAfterAuto = False
    .Borders.Shadow = False
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  End With
  With Selection.Find
    .Text = "speaker"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  With Selection
    .Find.Execute Replace:=wdReplaceAll
    If .Find.Forward = True Then
      .Collapse Direction:=wdCollapseEnd
      .Collapse Direction:=wdCollapseStart
    End If
  End With
End Sub

Open in new window

Andreas HermleTeam leaderAuthor Commented:
Dear all,

thank you very much for your swift help.
Although Chris' code seems logical to me, it regrettably does not work.
Dot office's code works as desired. Thank you very much
Dawn's solution is the simplest, so I guess I will award her the most points.

Again, thank you very much for your swift and great help.

Regards, Andreas
Andreas HermleTeam leaderAuthor Commented:
Thank you very much for your time taken and your professional help. Regards, Andreas
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now