View Single Post
 
Old 06-01-2021, 02:54 PM
macropod's Avatar
macropod macropod is offline Windows 10 Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

You have the right idea, but the wrong approach. Try:
Code:
Sub TagtoStyle()
Application.ScreenUpdating = False
Dim rngStory As Range, StrStry As String
For Each rngStory In ActiveDocument.StoryRanges
  With rngStory
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "\<[! ]@\>"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    If .Find.Found = False Then
      Select Case rngStory
        Case wdCommentsStory: StrStry = "Comments"
        Case wdEndnotesStory: StrStry = "Endnotes"
        Case wdFootnotesStory: StrStry = "Footnotes"
        Case wdMainTextStory: StrStry = "Document Body"
        Case Else: StrStry = ""
      End Select
      If StrStry <> "" Then MsgBox "No tags found in: " & StrStry, vbExclamation
    End If
    Do While .Find.Found = True
      .Style = Split(Split(.Text, "<")(1), ">")(0)
      .Text = vbNullString
      .Find.Execute
    Loop
  End With
Next rngStory
Application.ScreenUpdating = True
End Sub
Note that a slightly different approach is needed to include all headers & footers in a multi-section document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote