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: 163
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,956
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: 163
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,956
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: 163
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,956
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: 163
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,956
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: 163
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
  #25  
Old 09-16-2023, 11:58 PM
RobiNew RobiNew is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

I have copied and used with success this code by macropod:
Application.ScreenUpdating = False
Dim FtNt As Footnote
Dim Rng As Range
'Dim Fld As Field
With ActiveDocument
For Each FtNt In .Footnotes
Set Rng = FtNt.Reference
With Rng
'Delete spaces etc.
Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
.Characters.First.Previous.Text = vbNullString
Loop
'Invert ref. number and punctuation
'MsgBox .Characters.First.Previous
Do While Not .Characters.First.Previous Like "[0-9A-Za-z]" And Not .Characters.First.Previous Like "‹" And Not .Characters.First.Previous Like "«"
.InsertAfter .Characters.First.Previous
.Characters.First.Previous.Delete
Loop
End With
Next
End With
Thanks a lot for this!
My problem is that I want the inversion to occur only in paragraphs with no left indent. How to I modify the code so that left-indented paragraphs are skipped? Thank you for your time!
Reply With Quote
  #26  
Old 09-21-2023, 03:02 AM
Guessed's Avatar
Guessed Guessed is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

RobiNew
You've piggybacked on someone else's thread and it isn't clear what it is that you are trying to achieve. Maybe explain what the code is supposed to find and what it needs to do when it finds that. And post a sample document that shows the 'before' state that you want the code to work on.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #27  
Old 09-21-2023, 08:13 AM
RobiNew RobiNew is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

I apologize! I'm new to these procedures. My aim is to invert reference numbers and punctuation. This I obtained with Macropod's code here below (slightly adapted with regard to some special quotation marks). However, in the text some of the paragraphs involved are left-indented and in these I don't want the inversion to operate. How do I modify (or add to) the code so that the sequence 'punctuation-ref.number' is left untouched in left-indented paragraphs? Thank you for your time!


This is the code I adapted:

Quote:
Application.ScreenUpdating = False
Dim FtNt As Footnote
Dim Rng As Range
With ActiveDocument
For Each FtNt In .Footnotes
Set Rng = FtNt.Reference
With Rng
'Delete spaces etc.
Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
.Characters.First.Previous.Text = vbNullString
Loop
'Invert ref. number and punctuation
Do While Not .Characters.First.Previous Like "[0-9A-Za-z]" And Not .Characters.First.Previous Like "‹" And Not .Characters.First.Previous Like "«"
.InsertAfter. Characters.First.Previous
.Characters.First.Previous.Delete
Loop
End With
Next
End With
Reply With Quote
  #28  
Old 09-21-2023, 03:44 PM
Guessed's Avatar
Guessed Guessed is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Maybe try this modification
Code:
Sub ReconfigFootnotes()
  Dim FtNt As Footnote, Rng As Range
  With ActiveDocument
    For Each FtNt In .Footnotes
      Set Rng = FtNt.Reference
      With Rng
        If Rng.Paragraphs(1).LeftIndent = 0 Then
          'Delete spaces etc.
          Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
            .Characters.First.Previous.Text = vbNullString
          Loop
          'Invert ref. number and punctuation
          Do While Not .Characters.First.Previous Like "[0-9A-Za-z]" And Not .Characters.First.Previous Like "‹" And Not .Characters.First.Previous Like "«"
            .InsertAfter .Characters.First.Previous
            .Characters.First.Previous.Delete
          Loop
        End If
      End With
    Next
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #29  
Old 09-22-2023, 12:17 AM
RobiNew RobiNew is offline VBA Move Footnote References Before Punctuation Windows 10 VBA Move Footnote References Before Punctuation Office 2016
Competent Performer
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Smile

Many thanks, Guessed! That is simple and perfect.
Now I'm trying to devise a macro to convert all strings in italics to an italics style. But all I can get is a very long and clumsy code sequence. Is there a simple way to do it?
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 05:38 AM.


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