Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 08-24-2022, 12:19 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 118
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod thank you for your suggestion - I replaced with .Delete but that gave an error 5904 cannot edit range.
Reply With Quote
  #17  
Old 08-24-2022, 08:32 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: 21,725
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

Try:
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
      If .Characters.First.Previous.Fields(1).Result = "]" Then
        .Start = .Characters.First.Previous.Fields(1).Result.Start + 1
      Else
        Exit Do
      End If
    End If
    If .Characters.First.Previous.ContentControls.Count = 1 Then
      If .Characters.First.Previous.ContentControls(1).Range.Text = "]" Then
        .Start = .Characters.First.Previous.ContentControls(1).Range.Start + 1
      Else
        Exit Do
      End If
    End If
    .Start = .Start - 1
  Loop
  'Swap the footnote/punctuation, as applicable
  If .Start <> Rng.Start Then
    Rng.Collapse wdCollapseEnd
    .Cut
    Rng.Paste
  End If
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #18  
Old 08-25-2022, 08:13 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 118
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod, thank you so much for the new code, I've run it on a 153 page contract today and all looking good - is there a way to tell the code to ignore "quotes", apostrophe's and (parenthesis) as its moving the Footnote Reference in the wrong place, it only needs to move for periods, semi-colons, colons and question marks
Reply With Quote
  #19  
Old 08-25-2022, 02:39 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: 21,725
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

Simply edit:
Do While .Characters.First.Previous Like "[!0-9A-Za-z" & vbCr & Chr(11) & vbTab & "]"
For example:
Do While .Characters.First.Previous Like "[!0-9A-Za-z,’”)" & vbCr & Chr(11) & vbTab & "]"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #20  
Old 08-26-2022, 07:58 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 118
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod, thanks for the update, I got a Syntax error so I changed the code to:

Code:
Do While .Characters.First.Previous Like "[!0-9A-Za-z" & Chr(39) & Chr(34) & Chr(41) & vbCr & Chr(11) & vbTab & "]"
And that seems to have worked.

Thank you so much for your help, really appreciate it.

Before I close this thread as solved - I've got some rogue spaces between the footnote reference and punctuation after running the code - I've tried adding the following at the end of the Sub SetPunctAfter macro but it doesn't seem to be working - it works when I do a normal find and replace but not when in the code.

Code:
With Rng.Find
.text = "(^2)[ ]([.,:;\?\!])"
.Replacement.text = "\1\2"
.Execute Replace:=wdReplaceAll
End With
Reply With Quote
  #21  
Old 08-26-2022, 02:49 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: 21,725
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

Simply insert:

Code:
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
End with
at the end of the SetPunctAfter sub.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #22  
Old 09-02-2022, 12:24 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 118
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Hi Macropod, unfortunately that didn't work as its looking for a space before the footnote reference - when I run the whole code I am getting instances where there is a space in between a footnote reference and punctuation, mainly at the end of paragraphs - I've checked that this wasn't present before running the whole code, not sure what to change the last bit of code to.

Capture.PNG
Reply With Quote
  #23  
Old 09-02-2022, 06:22 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: 21,725
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

OK, between the final 'Next' and 'End With', insert:
Code:
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "([ ^s]{1,})([,.;:\?\!^13^l^t])"
    .Replacement.Text = "\2"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #24  
Old 09-05-2022, 05:59 AM
Shelley Lou Shelley Lou is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
VBA Move Footnote References Before Punctuation
 
Join Date: Dec 2020
Posts: 118
Shelley Lou is on a distinguished road
Default VBA Footnote References Before Punctuation

Thanks so much Macropod that seems to have fixed the issue
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 01:57 AM.


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