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 HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.