#1
|
|||
|
|||
VBA Move Footnote References Before Punctuation
I have been given quite a few 200 page contract documents to house style/format each containing over 300 footnotes. I recorded a find and replace to move the punctuation before the footnotes refs - but when I added code to remove spaces before footnotes refs it stopped working.
Code:
.text = "([.,:;\?\!])(^2)" What have I missed from the code to make it work correctly. Capture.PNG Code:
Sub MoveFootnotesBeforePunctuation() Dim oRng As Range, oFN As Footnote For Each oFN In ActiveDocument.Footnotes 'Remove spaces before footnote refs Do While oFN.Reference.Characters.First.Previous Like "[" & Chr(32) + Chr(160) & "]" oFN.Reference.Characters.First.Previous.Delete Loop Next Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .text = "([.,:;\?\!])(^2)" 'Move footnote refs before punctuation .Replacement.text = "\2\1" .text = "([\]])(^2)(^13)" 'Move footnote refs before square brackets end of paras .Replacement.text = "\2\1\3" .text = "([.;:])([\]])(^2)(^13)" 'Move footnote refs before square brackets and punctuation .Replacement.text = "\3\2\1\4" .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End With End Sub |
#2
|
||||
|
||||
To ensure punctuation etc. is placed before footnotes, endnotes and cross-references to them, use:
Code:
Sub FootnoteEndnotePunctBef() Application.ScreenUpdating = False Dim FtNt As Footnote, EndNt As Endnote, 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 EndNt In .Endnotes Set Rng = EndNt.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 Code:
Sub FootnoteEndnotePunctAft() Application.ScreenUpdating = False Dim FtNt As Footnote, EndNt As Endnote, 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 Not .Characters.First.Previous Like "[0-9A-Za-z]" .InsertAfter .Characters.First.Previous .Characters.First.Previous.Delete Loop End With Next For Each EndNt In .Endnotes Set Rng = EndNt.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 Not .Characters.First.Previous Like "[0-9A-Za-z]" .InsertAfter .Characters.First.Previous .Characters.First.Previous.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.First.Previous Like "[0-9A-Za-z]" .InsertAfter .Characters.First.Previous .Characters.First.Previous.Delete Loop End With End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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 |
#4
|
||||
|
||||
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] |
#5
|
|||
|
|||
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 & "]" |
#6
|
||||
|
||||
The Like expression:
Code:
"[!0-9A-Za-z" & vbCr & Chr(11) & vbTab & "]" PS: Code updated to make cross-reference handling more robust.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
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] |
#8
|
|||
|
|||
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 |
#9
|
||||
|
||||
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] |
#10
|
|||
|
|||
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.
|
#11
|
||||
|
||||
Quote:
Replace: Code:
If .Characters.First.Previous.Fields.Count = 1 Then Exit Do If .Characters.First.Previous.ContentControls.Count = 1 Then Exit Do 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] |
#12
|
|||
|
|||
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 |
#13
|
||||
|
||||
Try:
Code:
If .Characters.First.Previous.Fields.Count = 1 Then If .Characters.First.Previous.Fields(1).Result <> "]" Then Exit Do End If If .Characters.First.Previous.ContentControls.Count = 1 Then If .Characters.First.Previous.ContentControls(1).Range.Text <> "]" Then Exit Do End If
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
VBA Footnote References Before Punctuation
Hi Macropod, thank you for the updated code - I added this in but got an error 6028 range cannot be detected
Code:
.text = vbNullString Code:
.Start = .Start - 1 |
#15
|
||||
|
||||
You might try replacing:
.Text = vbNullString with: .Delete
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Footnote references in the footnote section losing their style when cut+pasted from same doc | emblaw | Word | 4 | 12-08-2020 06:23 AM |
Fix footnote and endnote references to arabic numbers everywhere? | KDuncan | Word | 6 | 04-28-2020 12:14 AM |
Word Find won't move out of footnote | michaelbriordan | Word | 3 | 06-17-2015 10:12 AM |
How do I keep footnote references and text on the same page | bearligirl89 | Word | 3 | 11-20-2013 03:33 PM |
Multiple footnote references | jimgard | Word | 1 | 07-23-2013 11:47 AM |