Skip to content

Instantly share code, notes, and snippets.

@johndyer
Last active July 3, 2020 16:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndyer/f7c5881f6a1752fcc1a307f6e2c647d0 to your computer and use it in GitHub Desktop.
Save johndyer/f7c5881f6a1752fcc1a307f6e2c647d0 to your computer and use it in GitHub Desktop.
Visual Basic Macro for moving EndNote citations from footnotes inline
Sub MoveFootnoteCitationInline()
' declare variable
Dim oFeets As Footnotes
Dim oFoot As Footnote
Dim oRange As Range
Dim szFootNoteText As String
Dim index As Long
Dim oFootRefRange1 As Range
Dim oFootRefRange2 As Range
Dim quoteText As String
Dim movedNotes As Integer
movedNotes = 0
' Get a collection of FootNotes
Set oFeets = ActiveDocument.Footnotes
' Go through each footnote
For Each oFoot In oFeets
index = index + 1
szFootNoteText = oFoot.Range.Text
' Reset the selection range
Set oRange = ActiveDocument.Range
With oRange.Find
.Text = "^f" ' Looks for all footnotes
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Debug.Print (index & " " & szFootNoteText)
' Look for footnotes that start with Endnote's { in order to ignore footnotes that are just text
If szFootNoteText Like "{*" Then
movedNotes = movedNotes + 1
' Create ranges to figure out where to move the reference
Set oFootRefRange1 = oFoot.Reference
Set oFootRefRange2 = oFoot.Reference
With oFootRefRange1
.Move Unit:=wdCharacter, Count:=-2
.MoveEnd Unit:=wdCharacter, Count:=1
.Select
End With
With oFootRefRange2
.Move Unit:=wdCharacter, Count:=-3
.MoveEnd Unit:=wdCharacter, Count:=2
.Select
End With
' Below are several formats a footnote number could be in
' after a quotatation mark
' word.”1
If oFootRefRange2.Text = ".”" Then
Debug.Print (" => fix period .”1 ")
With oFootRefRange2
.MoveEnd Unit:=wdCharacter, Count:=2
.Select
End With
oFootRefRange2.Text = "” " & Trim(szFootNoteText) & ". "
' after a period or comma
' word.1 OR word,1
ElseIf oFootRefRange1.Text = "." Or oFootRefRange1.Text = "," Then
Debug.Print (" => before period or comma ")
With oFootRefRange1
.Collapse Direction:=wdCollapseStart
.Select
End With
oFootRefRange1.Text = " " & Trim(szFootNoteText)
oFoot.Delete
' any other inline place
Else
Debug.Print (" => insert ")
oFoot.Reference.Text = " " & Trim(szFootNoteText)
'oFoot.Delete
End If
Else
Debug.Print (" ++ KEEP")
'oFoot.Range.Text = oFoot.Range.Text & " [keep]"
End If
' Disables undo to save memory on very large documents.
ActiveDocument.UndoClear
Next
Debug.Print ("Total:" & movedNotes)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment