View Single Post
 
Old 08-18-2022, 03:17 PM
macropod's Avatar
macropod macropod is offline Windows 10 Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

The ] before the 42, for example, is a formfield. It seems to me there are more fundamental problems with your document than just the footnote references. It is not at all clear to me why you would have such formfields or how the code should determine whether to relocate a reference to before a formfield.

In the meantime, here's a more refined version of the code:
Code:
Sub EndNoteFootNotePunctCheck()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
  'Process EndNotes
  For i = .Endnotes.Count To 1 Step -1
    Call SetPunctAfter(.Endnotes(i).Reference)
  Next
  'Process FootNotes
  For i = .Footnotes.Count To 1 Step -1
    Call SetPunctAfter(.Footnotes(i).Reference)
  Next
  'Process EndNote/FootNote References
  For i = .Range.Fields.Count To 1 Step -1
    With .Range.Fields(i)
      If .Type = wdFieldNoteRef Then
        Call SetPunctAfter(.Result)
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub

Sub SetPunctAfter(Rng As Range)
With Rng.Duplicate
  .Collapse wdCollapseStart
  'Eliminate any spaces before the footnote reference
  Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
    .Characters.First.Previous.Text = vbNullString
  Loop
  'Find the preceding puctuation, bracket, etc.
  Do While .Characters.First.Previous Like "[!0-9A-Za-z" & vbCr & Chr(11) & vbTab & "]"
    If .Characters.First.Previous.Fields.Count = 1 Then Exit Do
    If .Characters.First.Previous.ContentControls.Count = 1 Then Exit Do
    .Start = .Start - 1
  Loop
  'Swap the footnote/punctuation, as applicable
  If .Start <> Rng.Start Then
    Rng.Collapse wdCollapseEnd
    Rng.FormattedText = .FormattedText
    .Text = vbNullString
  End If
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote