Converting in-text citations to footnotes/endnotes

Hi all …

I needed to convert in-text refs like (Jones 2010:23) or (Peters 2011:19-23) to footnote refs of the format

…, p. 23.

…, pp. 19-23.

and if the in-text citation does not show the author, then switch author on in the footnote

This macro is a modification of the one posted by Miriam way back in 2009 from one created by Shyam Ranganathan.

I have removed redundant code (which you get when you record a macro), and added comments so those not too familiar with VBA can see what’s happening.

You can un-comment the message boxes (just delete ’ in 'MsgBox …) to see what’s happening.

The code could be improved further, but it works OK … at least for me (Word 2007, Windows 7).

Happy conversions!!!

PS: To make this work for Endnotes just change ActiveDocument.Footnotes.Add to ActiveDocument.Endnotes.Add


Public Sub ConvertIntextToFootnote()

Dim oField As Field
Dim sCode As String
Dim sSuffix As String
Dim sSuffixLeft As String
Dim sSuffixRight As String
Dim pos1 As Long
Dim pos2 As Long

'Loop through all fields in the document

For Each oField In ActiveDocument.Fields

    'Is the field an Endnote field?
    
    If InStr(oField.Code.Text, “EN.CITE”) = 0 Then GoTo Bypass:
    
    oField.Select
    
    'Modify the Suffix in the field code … <Suffix>text</Suffix>, if one exists

    sCode = oField.Code.Text
    'MsgBox sCode
    pos1 = InStr(sCode, “<Suffix>”)
    
    If pos1 > 0 Then 'suffix exists, so extract it
    
        sSuffixLeft = Left(sCode, pos1 + 7)
        'MsgBox sSuffixLeft
        pos2 = InStr(sCode, “</Suffix>”)
        sSuffixRight = Right(sCode, Len(sCode) - pos2 + 1)
        'MsgBox sSuffixRight
        sSuffix = Mid(sCode, pos1 + 8, pos2 - (pos1 + 8))
        'MsgBox sSuffix
        
        'If no dash, replace : with p. else with  pp.

        If InStr(sSuffix, “-”) > 0 Then
            sSuffix = Replace(sSuffix, “:”, " pp. ")
        Else
            sSuffix = Replace(sSuffix, “:”, " p. ")
        End If
        
        'Add a full stop

        sSuffix = sSuffix + “.”
        'MsgBox sSuffix
        
        'Now rebuild the code

        sCode = sSuffixLeft + sSuffix + sSuffixRight

        'Set the new code in the field

        oField.Code.Text = sCode
        'MsgBox oField.Code.Text
        
    End If
    
     'If author is excluded, show the author
        
        If InStr(sCode, “ExcludeAuth=”“1"”") > 0 Then
        
            oField.Code.Text = Replace(sCode, “ExcludeAuth=”“1"”", “”)
            
        End If
    
    Selection.Cut
    
    'Add temporary bookmark so we can return to the Main Story

    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:=“tempqxtz”
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
            
    'Add the Footnote

    ActiveDocument.Footnotes.Add Range:=Selection.Range, Reference:=""
    Selection.Paste
    
    'Return to Main Story

    Selection.GoTo What:=wdGoToBookmark, Name:=“tempqxtz”
    
    'Delete the temporary bookmark

    ActiveDocument.Bookmarks(“tempqxtz”).Delete

Bypass:
    
Next oField

MsgBox “Process Complete”, vbOKOnly + vbInformation, “Convert In-text Citations to Footnotes”

End Sub


1 Like