none
Search and Replace within the Footers RRS feed

  • Question

  • We use styleref's in our footers to designate specific products, part of the naming convention is adding the registered® and unregistered™ marks. I'm trying to create a macro that with update the registered mark with a specific style and Superscript it. The macro will change the first couple of pages, then as it loops, it is updating sporadically. End result is not all pages are being updated. The macro runs, loops and closes OK. This is my first, from scratch and needing help. Thanks

    Sub FixFooterTradeMarks()
    
        Dim wdHeaderFooterIndex
        Dim wdHeaderFooterFirstPage
        Dim wdHeaderFooterEvenPages
        Dim wdHeaderFooterOddPages
        Dim wdEvenPagesFooter
        Dim wdFirstPageFooter
        Dim wdOddAndEvenPagesHeaderFooter
        Dim wdlnHeaderFooter
        Dim wdPrimaryfooter
        Dim wdSectionContinuous
        Dim wdSectionStart
        Dim wdSectionEvenPage
        Dim wdSectionOddPage
        
      
        
        Selection.GoTo What:=wdGoToPage, Which:=FirstPage
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    
        Selection.Find.ClearFormatting
        Selection.Find.Style = ActiveDocument.Styles("Footer")
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Style = ActiveDocument.Styles("SymbolsTM")
        With Selection.Find
            .Text = "®"
            .Replacement.Text = "®"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
        End With
       
    If Selection.Type = wdSelectionSymbolsTM Then
        Selection.Font.SuperScript = True
       End If
        Selection.Find.Execute Replace:=wdReplaceAll
          
    Do
        Do While Counter < 100
        Counter = Counter + 1
            If Counter = 70 Then
            Check = False
           Exit Do
         End If
    
            Selection.GoTo What:=wdGoToPage, Which:=NextPage
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    
            Selection.Find.ClearFormatting
            Selection.Find.Style = ActiveDocument.Styles("Footer")
            Selection.Find.Replacement.ClearFormatting
            Selection.Find.Replacement.Style = ActiveDocument.Styles("SymbolsTM")
            
          With Selection.Find
            .Text = "®"
            .Replacement.Text = "®"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
          End With
    
    If Selection.Type = wdSelectionSymbolsTM Then
        Selection.Font.SuperScript = True
       End If
        Selection.Find.Execute Replace:=wdReplaceAll
        
        Selection.GoTo What:=wdHeaderFooterIndex, Which:=NextPage
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        
      Loop
        Loop Until Check = False
     
    End Sub

    Thursday, October 5, 2017 8:45 PM

Answers

  • Hi Paul,
    I appreciate the comments and feedback, I've used a combination of multiple macros to piece together some code that actually works. Yea! I've posted it for your input and comments, if you like.

    Thanks again..

    Public Sub FixTradeMarkFooters()
    
        Dim rngStory   As Word.Range
        Dim lngJunk   As Long
    
        
    
        lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
        
        Dim wdEvenPagesFooterStory
        Dim wdEvenPagesHeaderStory
        Dim wdFirstPageFooterStory
        Dim wdFirstPageHeaderStory
        Dim wdPrimaryFooterStory
        Dim wdPrimaryHeaderStory
        Dim wdFootnotesStory
        Dim wdMainTextStory
        Dim wdTextFrameStory
        
        Selection.GoTo What:=wdGoToPage, Which:=FirstPage
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    
        For Each rngStory In ActiveDocument.StoryRanges
    
    
          Do
    
          With rngStory.Find
            .Style = ActiveDocument.Styles("Footer")
            .Text = "®"
            .Replacement.Text = "®"
            .Replacement.Style = ActiveDocument.Styles("SymbolsTM")
            .Wrap = wdFindContinue
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .Execute Replace:=wdReplaceAll
    
            End With
    
            If Selection.Type = wdSelectionSymbolsTM Then
            Selection.Font.SuperScript = True
       End If
        Selection.Find.Execute Replace:=wdReplaceAll
        
            Selection.GoTo What:=wdGoToPage, Which:=NextPage
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
            
            Set rngStory = rngStory.NextStoryRange
            
          Loop Until rngStory Is Nothing
    
      Next
    End Sub
    

    Monday, October 9, 2017 2:09 PM
  • Well, I already showed you what decent code would look like - and demonstrated that there is no need for the header/footer pane to be opened. You also don't need to select anything. These are very inefficient methods. That said, I do concede there was an error in my original code. Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim RngStory As Range, IntPageNo As Long
    Dim Sctn As Section, HdFt As HeaderFooter
    For Each Sctn In ActiveDocument.Sections
      For Each HdFt In Sctn.Footers
        With HdFt
          If .Exists Then
            With .Range.Find
              .ClearFormatting
              With .Replacement
                .ClearFormatting
                .Text = "^&"
                .Style = "SymbolsTM"
              End With
              .Format = True
              .Forward = True
              .Wrap = wdFindContinue
              .Text = "®"
              .Execute Replace:=wdReplaceAll
              .Text = "™"
              .Execute Replace:=wdReplaceAll
            End With
          End If
        End With
      Next
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Tuesday, October 10, 2017 7:19 PM
    • Marked as answer by CodeChallenged Tuesday, October 10, 2017 8:30 PM
    Monday, October 9, 2017 11:45 PM

All replies

  • Unless you're using your 'SymbolsTM' Style for something else, the superscripting should already be part of the Style's definition; otherwise, create another Style based on 'SymbolsTM' but with superscripting applied and use that. Subject to that proviso:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim RngStory As Range, IntPageNo As Long
    Dim Sctn As Section, HdFt As HeaderFooter
    For Each Sctn In ActiveDocument.Sections
      For Each HdFt In Sctn.Footers
        With HdFt
          If .Exists Then
            If .LinkToPrevious = False Then
              With .Range.Find
                .ClearFormatting
                With .Replacement
                  .ClearFormatting
                  .Text = "^&"
                  .Style = "SymbolsTM"
                End With
                .Format = False
                .Forward = True
                .Wrap = wdFindContinue
                .Text = "®"
                .Execute Replace:=wdReplaceAll
                .Text = "™"
                .Execute Replace:=wdReplaceAll
              End With
            End If
          End If
        End With
      Next
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, October 6, 2017 12:45 AM
  • I appreciate the help on this one. When I step into the macro, It jumps from (If .LinkToPrevious = False Then) to (End If) Skipping the code, when I changed the statement to True, the Macro runs, but won't update the footers. It appears that the 'Headers/Footers' pane is not opening for editing. When I open the footers and run the macro its still not updating. I am using 'SymbolsTM' only for the footer ® and yes superscript is applied. The Symbols style is applied to the ® it Author-it and Published to WORD in a string styled with ProductName.

    Is part of the problem that the footer is built with a styleref and the style is ProductName/Merge Formatting with the footer style and now I'm trying to locate the ® symbol within the footer to update/superscript?

    Thanks

    Friday, October 6, 2017 2:27 PM
  • 1. The 'If .LinkToPrevious = False Then' merely spares the code the unnecessary effort of doing a Find/Replace on a footer linked to a previous one, as that footer will already have been updated.

    2. There is no need for the header/footer pane to be opened for the macro to do its work.

    3. If the symbols are part of a field's result, the whole exercise is a waste of time.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, October 6, 2017 8:57 PM
  • Hi Paul,
    I appreciate the comments and feedback, I've used a combination of multiple macros to piece together some code that actually works. Yea! I've posted it for your input and comments, if you like.

    Thanks again..

    Public Sub FixTradeMarkFooters()
    
        Dim rngStory   As Word.Range
        Dim lngJunk   As Long
    
        
    
        lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
        
        Dim wdEvenPagesFooterStory
        Dim wdEvenPagesHeaderStory
        Dim wdFirstPageFooterStory
        Dim wdFirstPageHeaderStory
        Dim wdPrimaryFooterStory
        Dim wdPrimaryHeaderStory
        Dim wdFootnotesStory
        Dim wdMainTextStory
        Dim wdTextFrameStory
        
        Selection.GoTo What:=wdGoToPage, Which:=FirstPage
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    
        For Each rngStory In ActiveDocument.StoryRanges
    
    
          Do
    
          With rngStory.Find
            .Style = ActiveDocument.Styles("Footer")
            .Text = "®"
            .Replacement.Text = "®"
            .Replacement.Style = ActiveDocument.Styles("SymbolsTM")
            .Wrap = wdFindContinue
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .Execute Replace:=wdReplaceAll
    
            End With
    
            If Selection.Type = wdSelectionSymbolsTM Then
            Selection.Font.SuperScript = True
       End If
        Selection.Find.Execute Replace:=wdReplaceAll
        
            Selection.GoTo What:=wdGoToPage, Which:=NextPage
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
            
            Set rngStory = rngStory.NextStoryRange
            
          Loop Until rngStory Is Nothing
    
      Next
    End Sub
    

    Monday, October 9, 2017 2:09 PM
  • That's truly awful code. Seriously.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, October 9, 2017 8:29 PM
  • I was hoping for sum constructive input, it actually works so I'm not sure what is truly awful about it.

    I guess this is not a place to get help,

    Thanks anyways

    Monday, October 9, 2017 8:50 PM
  • Well, I already showed you what decent code would look like - and demonstrated that there is no need for the header/footer pane to be opened. You also don't need to select anything. These are very inefficient methods. That said, I do concede there was an error in my original code. Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim RngStory As Range, IntPageNo As Long
    Dim Sctn As Section, HdFt As HeaderFooter
    For Each Sctn In ActiveDocument.Sections
      For Each HdFt In Sctn.Footers
        With HdFt
          If .Exists Then
            With .Range.Find
              .ClearFormatting
              With .Replacement
                .ClearFormatting
                .Text = "^&"
                .Style = "SymbolsTM"
              End With
              .Format = True
              .Forward = True
              .Wrap = wdFindContinue
              .Text = "®"
              .Execute Replace:=wdReplaceAll
              .Text = "™"
              .Execute Replace:=wdReplaceAll
            End With
          End If
        End With
      Next
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Tuesday, October 10, 2017 7:19 PM
    • Marked as answer by CodeChallenged Tuesday, October 10, 2017 8:30 PM
    Monday, October 9, 2017 11:45 PM
  • Hi Paul,

    Thank you for your input and patience. This current code you sent me works perfectly and I will use this as my bench mark going forward. This is my first code and it has been a great learning experience.

    Cheers!
    John

    Tuesday, October 10, 2017 1:55 PM