Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 08-17-2022, 04:00 PM
macropod's Avatar
macropod macropod is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,384
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

In your sample document, you have a formfield immediately after the first footnote reference. The code was not written with such documents in mind. As written, the code moves each character after the footnote reference individually, which is obviously not possible the formfields. For your document, the code needed to be completely re-written to process all the references in reverse order and move the actual references instead of the associated text:
Code:
Sub FootnotePunctBefore()
Application.ScreenUpdating = False
Dim bHid As Boolean, i As Long, Rng As Range, StrBkMkNm As String
With ActiveDocument
  bHid = .Bookmarks.ShowHidden
  .Bookmarks.ShowHidden = True
  For i = .Footnotes.Count To 1 Step -1
    With .Footnotes(i)
      With .Reference
        Set Rng = .Duplicate
        If .Bookmarks.Count = 0 Then
          StrBkMkNm = ""
        Else
          StrBkMkNm = .Bookmarks(1).Name
        End If
      End With
      With Rng
        .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
          .End = .End - 1
        Loop
      End With
      'Swap the footnote/punctuation, as applicable
      If .Reference.Start <> Rng.Start Then
        Rng.FormattedText = .Reference.FormattedText
        If StrBkMkNm <> "" Then .Range.Bookmarks.Add StrBkMkNm, Rng
        .Delete
      End If
    End With
  Next
  For i = .Range.Fields.Count To 1 Step -1
    With .Range.Fields(i)
      If .Type = wdFieldNoteRef Then
        Set Rng = .Result
        With Rng
          .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
            .End = .End - 1
          Loop
        End With
        'Swap the footnote/punctuation, as applicable
        If .Result.Start <> Rng.Start Then
          Rng.FormattedText = .Result.FormattedText
          .Delete
        End If
      End If
    End With
  Next
  .Bookmarks.ShowHidden = bHid
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Move Footnote References Before Punctuation Footnote references in the footnote section losing their style when cut+pasted from same doc emblaw Word 4 12-08-2020 06:23 AM
VBA Move Footnote References Before Punctuation Fix footnote and endnote references to arabic numbers everywhere? KDuncan Word 6 04-28-2020 12:14 AM
VBA Move Footnote References Before Punctuation Word Find won't move out of footnote michaelbriordan Word 3 06-17-2015 10:12 AM
VBA Move Footnote References Before Punctuation How do I keep footnote references and text on the same page bearligirl89 Word 3 11-20-2013 03:33 PM
VBA Move Footnote References Before Punctuation Multiple footnote references jimgard Word 1 07-23-2013 11:47 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:53 AM.


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