View Single Post
 
Old 08-17-2022, 10:15 AM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod, thank you for providing the code. I am getting error 5904 on the first .Characters.Last.Next.Delete - should I be changing [!0-9A-Za-z] to [.,;:]? I have attached a stripped down version of a few pages of the contracts I am working on for ease. I only need to move the footnote before a square bracket field if they appear at the end of paragraphs and not within the paragraph itself.

footnote test doc.docx

Code:
Sub FootnotePunctBefore()
Application.ScreenUpdating = False
Dim FtNt As Footnote, Rng As Range, Fld As Field
With ActiveDocument
  For Each FtNt In .Footnotes
    Set Rng = FtNt.Reference
    With Rng
      'Eliminate any spaces before the footnote
      Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
        .Characters.First.Previous.text = vbNullString
      Loop
      'Swap the footnote/punctuation, as applicable
      Do While .Characters.Last.Next Like "[!0-9A-Za-z]"
        .InsertBefore .Characters.Last.Next
        .Characters.Last.Next.Delete
      Loop
    End With
  Next
  For Each Fld In .Range.Fields
    With Fld
      If .Type = wdFieldNoteRef Then
        'Swap the footnote/punctuation, as applicable
        With .Result
          Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
            .Characters.First.Previous.text = vbNullString
          Loop
          'Swap the footnote/punctuation, as applicable
          Do While Not .Characters.Last.Next Like "[0-9A-Za-z]"
            .InsertBefore .Characters.Last.Next
            .Characters.Last.Next.Delete
          Loop
        End With
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote