[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

Word macro to convert footnotes and endnotes to anchors and links

I have a Word document with footnotes in it.They are usually in the form of superscript numbers, but can be written as normal case numbers. The endnotes can also be in either form.

This macro converts footnotes in the form [64] to an anchor and link to an endnote and converts endnotes in a similar form to anchors and links back to the corresponding footnote. The endnote block has to be coloured red for this to work, but it works well.

How can I adapt the macro to find either 64 or (superscript) 64, convert it to [64] and insert the links and anchors?
0
bogorman
Asked:
bogorman
  • 10
  • 8
1 Solution
 
GrahamSkanCommented:
If you already have a macro, can I suggest that you post it here and then we will know what we are trying to adapt?
Also what is 64 in this context?
0
 
bogormanAuthor Commented:
Hi Graham,
Sorry I omitted the macro coding. It is:

Sub Footnotes()
'
' Footnotes Macro
' Macro created 5/27/2007 by Brian O'Gorman
'

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Options.AutoFormatAsYouTypeReplaceQuotes = False
With Selection.Find
.Text = "(\[)([0-9]@)(\])"
.Forward = True: .Wrap = wdFindContinue: .Format = False
.MatchCase = False: .MatchWholeWord = False: .MatchAllWordForms = False
.MatchSoundsLike = False: .MatchWildcards = True
End With
While Selection.Find.Execute
If MsgBox("Should I replace", vbYesNo) = vbYes Then
If Selection.Font.Color <> vbRed Then
Selection.Find.Replacement.Text = _
"<a name=""fn\2"" id=""fn\2""></a><a href=""#en\2"">[\2]</a>"
Else
Selection.Find.Replacement.Text = _
"<a name=""en\2"" id=""en\2""></a><a href=""#fn\2"">[\2]</a>"
End If
Selection.Find.Execute Replace:=wdReplaceOne
Selection.Find.Execute
End If
Wend

End Sub

I took 64 as an example footnote, e.g. footnote number 64.
The macro does the conversion of the numbers to footnotes and endnotes and inserts the necessary anchors. The disadvantage is that I have to colour the endnote section red (no problem as it only takes a second or so) but mainly that I have to search through the document and surround each number with square brackets. Also, if the notes are in superscript, it does not convert them to normal size.
So, I would like to adapt the macro to:

When each link is found:
 i) if in superscript form, convert it to normal size
 ii) surround the number with square brackets
 iii) create the anchors and links, after asking "should I replace?..."  (which the present macro does)

Pse let me know if you need any other information
Brian

0
 
GrahamSkanCommented:
Sorry about the delay. As I said elsewhere a day or two ago, this one had slipped past me and since then I lost my system.

This code does the footnotes
Sub Footnotes()
    Dim ftn As Footnote
    Dim rng As Range
    
    For Each ftn In ActiveDocument.Footnotes
        ftn.Range.Select
        If MsgBox("Should I replace", vbYesNo) = vbYes Then
            ftn.Range.Text = "<a name=""fn\2"" id=""fn\2""></a><a href=""#en\2"">[\2]</a>"
        End If
        Set rng = ftn.Range
        rng.Collapse wdCollapseStart
        rng.MoveStart wdCharacter, -1
        Do Until rng.Start = 0
            rng.Start = rng.Start - 1
            If Asc(rng.Text) = 2 Then
                rng.Collapse wdCollapseStart
                rng.InsertBefore "["
                rng.End = rng.End + 1
                rng.Font.Superscript = False
                rng.Collapse wdCollapseEnd
                rng.InsertAfter "]"
                Exit Do
            End If
        Loop
    Next ftn
End Sub

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GrahamSkanCommented:
And try this for the EndNotes
Sub EndNotes()
    Dim endn As Endnote
    Dim rng As Range
    
    For Each endn In ActiveDocument.EndNotes
        endn.Range.Select
        If MsgBox("Should I replace", vbYesNo) = vbYes Then
            endn.Range.Text = "<a name=""en\2"" id=""en\2""></a><a href=""#fn\2"">[\2]</a>"
        End If
        Set rng = endn.Range
        rng.Collapse wdCollapseStart
        rng.MoveStart wdCharacter, -1
        Do Until rng.Start = 0
            rng.Start = rng.Start - 1
            If Asc(rng.Text) = 2 Then
                rng.Collapse wdCollapseStart
                rng.InsertBefore "["
                rng.End = rng.End + 1
                rng.Font.Superscript = False
                rng.Collapse wdCollapseEnd
                rng.InsertAfter "]"
                Exit Do
            End If
        Loop
    Next endn
End Sub

Open in new window

0
 
bogormanAuthor Commented:
Hi Graham,
Sorry for the delay.
Have tried the macros but they don't seem to run.
In fact, have renamed them FootnotesOnly and EndnotesOnly. Created macros with these names and copied the "body" of your coding (between SubFootnotes() and End Sub into the newly created coding. When I click run, nothing happens.
Will be grateful for your further advice.
Brian
0
 
GrahamSkanCommented:
Hi Brian,
It's hard to know exactly why. They should ask a question for each note in the document. Have you tried stepping through?

This new code should first tell you how many footnotes there are in the document.


Sub FootnotesOnly2()
    Dim ftn As Footnote
    Dim rng As Range
    Dim f As Integer
    MsgBox "There are " & ActiveDocument.Footnotes.Count & " footnotes in the active document, " & ActiveDocument.Name
    For Each ftn In ActiveDocument.Footnotes
        ftn.Range.Select
        If MsgBox("Should I replace", vbYesNo) = vbYes Then
            ftn.Range.Text = "<a name=""fn\2"" id=""fn\2""></a><a href=""#en\2"">[\2]</a>"
        End If
        Set rng = ftn.Range
        rng.Collapse wdCollapseStart
        rng.MoveStart wdCharacter, -1
        Do Until rng.Start = 0
            rng.Start = rng.Start - 1
            If Asc(rng.Text) = 2 Then
                rng.Collapse wdCollapseStart
                rng.InsertBefore "["
                rng.End = rng.End + 1
                rng.Font.Superscript = False
                rng.Collapse wdCollapseEnd
                rng.InsertAfter "]"
                Exit Do
            End If
        Loop
    Next ftn
End Sub

Open in new window

0
 
bogormanAuthor Commented:
Hi Graham,
I'm obviously doing something stupid.
The document I am trying it on is:

Now is1 the time for all good men to come to the aid of the party. Now is2 the time for all good men to come to the aid of the party. Now is the time for all good men to come to the aid of the 3party. Now is the time for all good men to come to the aid of the party.

In fact the first "1" has been set to superscript in Word but when I copy and paste it, it changes to normal integer format. When I run the macro it says "there are 0 footnotes.."

Regards
Brian
0
 
GrahamSkanCommented:
When you copy and paste text that includes the reference point for a real footnote, a new footnote is created and numbered according to its position in the document.
0
 
bogormanAuthor Commented:
Hi Graham,
Think you may have misunderstood me.
When I referred to copying and pasting, I referred to copying and pasting the text to this (Experts Exchange) comment.
Regards
Brian
0
 
GrahamSkanCommented:
O.K. That makes sense.

You could upload the zipped document here: www.ee-stuff.com, using your Experts-Exchange credentials . Identify the question with the URL or numerical part of the question ID. After a successful upload, you will be shown the URL to the file. Could you copy and paste it in a comment here, please?
0
 
bogormanAuthor Commented:
Hi Graham
Have just realised that I have been foolish and the reason your macro does not work is that the numbers in my documents are not footnotes/endnotes but simple numbers either in normal form or in superscript form.
Even though I copy them from a document with footnotes/endnotes in it, I copy them as plain text so they convert to numbers.
would you mind adapting your coding to read and convert numbers?
Very sorry for this mistake and for giving you extra work.
Brian
0
 
GrahamSkanCommented:
I think we might have to come back to something like your original solution code. The problem is going to be one of distinguishing the 'footnotes' and 'endnotes' from the rest of the text.
Endnotes are not such a problem since they will be at the end of the document, but what mechanism keeps the footnotes at the bottom of the page - are they followed by a manual page break? Perhaps you should post a sample on ee-stuff anyway.
0
 
bogormanAuthor Commented:
Hi Graham

To get the present macro to work I have to select the endnote block and color it red. That is how the macro distinguishes between footnotes and endnotes. It searches for an integer surrounded by square brackets (that means I have to go through all the document and replace surround the footnotes and the endnotes with such brackets - a real pain!). It then does the necessary replacement, using the different coding depending whether it is in the black area or the red area.

What I would like is for it to search for either a 'normal' integer, or one in superscript form. When it finds it, it replaces it with a 'normal' integer surrounded by square brackets. It then does the replacement.

Do you want me to send a sample of the DOCUMENT (with normal and superscripted 'notes'), or the present macro, or both.

Regards
Brian
0
 
GrahamSkanCommented:
Hi Brian,
It's the document that would help. You posted your macro in the beginning.
Cheers, Graham
0
 
bogormanAuthor Commented:
Hi Graham,
Have uploaded the files as you suggested.
Regards
Brian
0
 
GrahamSkanCommented:
The uploaded document to be treated shows a single page of what looks like EndNotes, except that they are simple text, not EndNotes in the Word sense.

The Text indexing is daone by appending a number at the end of a word, sometimes, but not always in superscript font. This presents a problem in that any combination of letters followed by digits in the text looks like an end note reference point.

"Yesterday1, the M1 junction with the M25 was gridlocked2" could yield four reference points, two of them valid, and two not.

I have tried to overcome this in the code by examining and validating the end notes themselves. Then the document text is checked to see that there are corresponding reference points in the correct sequence. Only after these checks are the changes made.
Sub ConvertTextualEndNotes()
    Dim wdPara As Paragraph
    Dim p As Integer
    Dim iLastEndnoteParaNo As Integer
    Dim iEndnoteCount As Integer
    Dim c As Integer
    Dim iParagraphCount As Integer
    Dim w As String
    Dim wdRange As Range
    
    iParagraphCount = ActiveDocument.Paragraphs.Count
    p = iParagraphCount
    Set wdPara = ActiveDocument.Paragraphs(p)
    Do Until Len(Trim$(wdPara.Range.Text)) > 1
        p = p - 1
        Set wdPara = ActiveDocument.Paragraphs(p)
    Loop
    w = Trim$(wdPara.Range.words(1).Text)
    If IsNumeric(w) Then
        iLastEndnoteParaNo = p
        iEndnoteCount = Val(w)
        c = iEndnoteCount
        Do
            Set wdPara = ActiveDocument.Paragraphs(p)
            w = Trim$(wdPara.Range.words(1).Text)
            Debug.Print wdPara.Range.Text
            If IsNumeric(w) Then
                If c <> w Then
                    MsgBox "EndNote Text sequence error"
                    Exit Sub
                End If
            Else
                MsgBox "EndNote Text sequence error"
                Exit Sub
            End If
            p = p - 1
            c = c - 1
            
        Loop Until c = 0
    Else
        MsgBox "No Endnotes found"
    End If
    Set wdRange = ActiveDocument.Range.Duplicate
    For c = 1 To iEndnoteCount
        With wdRange.Find
            .Text = "<[A-z]{1,}" & c & ">"
            .MatchWildcards = True
            Debug.Print wdRange.Start, wdRange.End
            If .Execute() = False Then
                MsgBox "No text referenced by endnote no: " & c
                Exit Sub
            End If
            Debug.Print wdRange.Start, wdRange.End
            wdRange.Collapse wdCollapseEnd
            wdRange.End = ActiveDocument.Range.End
        End With
    Next c
    Set wdRange = ActiveDocument.Range.Duplicate
    'checks done now do the changes
    For c = 1 To iEndnoteCount
        With wdRange.Find
            .Text = "(<[A-z]{1,})" & c & ">"
            .Replacement.Text = "\1<a name=""fn" & c & """ id=""fn" & c & """></a><a href=""#en" & c & """>[" & c & "]</a>"
            .MatchWildcards = True
            Debug.Print wdRange.Start, wdRange.End
            If .Execute(Replace:=wdReplaceOne) = False Then
                MsgBox "No text referenced by endnote no: " & c
                Exit Sub
            End If
            Debug.Print wdRange.Start, wdRange.End
            wdRange.Collapse wdCollapseEnd
            wdRange.End = ActiveDocument.Range.End
        End With
    Next c
    For c = 1 To iEndnoteCount
        Set wdPara = ActiveDocument.Paragraphs(iLastEndnoteParaNo - iEndnoteCount + c)
        Debug.Print wdPara.Range.Text
        wdPara.Range.words(1).Text = "<a name=""en" & c & """ id=""en" & c & """></a><a href=""#fn" & c & """>[" & c & "]</a>"
        wdPara.Range.Font.Superscript = False
        wdPara.Range.Font.Color = wdColorRed
    Next c
End Sub

Open in new window

0
 
bogormanAuthor Commented:
Hi Graham,
Absolutely brilliant!!! Sorry for delay in responding.
I had assumed that the coding would step through the text and select EVERY number and I would have to click NO (as the original macro did) but your one actually selects only the footnotes and endnotes.
This will save me hours of work.
Thanks so much.
Regards
Brian
0
 
GrahamSkanCommented:
Thanks Brian. Took a while, but we got there in the end.

Actually, I forgot to put the confirmation question in. Glad it wasn't needed.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now