Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-01-2021, 06:40 AM
stky stky is offline need to find in main text and footnote i tried but don't know where i mistake Windows 10 need to find in main text and footnote i tried but don't know where i mistake Office 2013
Advanced Beginner
need to find in main text and footnote i tried but don't know where i mistake
 
Join Date: Apr 2021
Posts: 30
stky is on a distinguished road
Default need to find in main text and footnote i tried but don't know where i mistake

Code:
Sub TagtoStyle()
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
Application.ScreenUpdating = False

With ActiveDocument.StoryRanges
  With rngStory.Find
  
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "\<[! ]@\>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  If .Find.Found = False Then
    MsgBox "No tags found", 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

Reply With Quote
  #2  
Old 06-01-2021, 02:54 PM
macropod's Avatar
macropod macropod is offline need to find in main text and footnote i tried but don't know where i mistake Windows 10 need to find in main text and footnote i tried but don't know where i mistake Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
need to find in main text and footnote i tried but don't know where i mistake How to perform (Find in Main Document) with VBA PassengerBird Word VBA 9 05-07-2021 01:36 PM
need to find in main text and footnote i tried but don't know where i mistake Removing line break and indentation between footnote number and footnote text in Word jaanross Word 5 02-06-2020 12:04 AM
need to find in main text and footnote i tried but don't know where i mistake Adding footnote number as part of footnote text NoCalScribe Word VBA 3 07-15-2019 07:20 PM
need to find in main text and footnote i tried but don't know where i mistake How do I get equal space between main body text and footnote separator line on every page? Different Word 1 02-09-2015 09:15 PM
Macro to find text only footnote numbers TimFromPhx Word VBA 7 04-10-2014 07:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft