Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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,383
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
  #2  
Old 08-18-2022, 01:39 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Expert
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod, thank you so much for taking the time to look at this issue and update the code - I've run the code on my document - the code hangs for a while but I'm guessing its because it is a 200 page document so takes time to read through but it doesn't seem
to move the footnote ref before the square bracket at the end of paragraphs so ]FN becomes FN] or .]FN becomes FN].

When I show the form field codes the square brackets are all FORMTEXT - I did try changing wdFieldNoteRef to wdFieldFormTextInput but that didn't work.

I'm curious to know what this line of code means and how the code picks up punctuation just from a learning point of view?

Code:
"[!0-9A-Za-z" & vbCr & Chr(11) & vbTab & "]"
Attachment 18328
Reply With Quote
  #3  
Old 08-18-2022, 06:26 AM
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,383
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

Quote:
Originally Posted by Shelley Lou View Post
it doesn't seem
to move the footnote ref before the square bracket at the end of paragraphs so ]FN becomes FN] or .]FN becomes FN].
What you're seeing there is due to the ] being part of the formfield text. One can hardly put a footnote reference inside a formfield!
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 08-18-2022, 09:12 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Expert
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod, I think we may have cross wires - nothing should happen to cross references - I was enquiring how to move the footnote references that appear at the end of paragraphs to be moved to the left of the form field not actually inside it - and where they appear at the end of the paragraph with punctuation the footnote ref to be moved before the punctuation.

Capture.PNG
Reply With Quote
Reply



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 04:59 PM.


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