Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2022, 03:17 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

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
  #2  
Old 08-19-2022, 03:57 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 your time and effort on this, it is very much appreciated. The contract documents I am working on are template precedent documents and our house style requires that all square brackets in template documents are form text fields so the user can F11 through them and remove as required. Your revised code unfortunately doesn't fix the issue - I did think a solution could be unlink the fields, move the footnotes then relink the form text fields - I will look into this further at a later date, for now I will just move the footnotes refs before punctuation when they appear without square brackets.
Reply With Quote
  #3  
Old 08-20-2022, 05:38 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
Your revised code unfortunately doesn't fix the issue - I did think a solution could be unlink the fields, move the footnotes then relink the form text fields
No, all I needed was an explanation of what the rules are so they could be coded for.

Replace:
Code:
    If .Characters.First.Previous.Fields.Count = 1 Then Exit Do
    If .Characters.First.Previous.ContentControls.Count = 1 Then Exit Do
with:
Code:
    If (.Characters.First.Previous.Fields.Count = 1) And (.Characters.First.Previous.Fields(1).Result <> "]") Then Exit Do
    If (.Characters.First.Previous.ContentControls.Count = 1) And (.Characters.First.Previous.ContentControls(1).Range.Text <> "]") Then Exit Do
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 08-22-2022, 06:16 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 have updated the code but am getting an error 5941 on this line:

Code:
If (.Characters.First.Previous.Fields.Count = 1) And (.Characters.First.Previous.Fields(1).Result <> "]") Then Exit Do
Reply With Quote
Reply

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 06:45 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