View Full Version : Insert reference line number in footnote/endnote
Hi. My first post - and of course it's a question!
I'm editing a 2400 line Word document with >100 footnotes, and want to create a separate report that gives the line number for the sentences being footnoted.
For example, Footnote 12 refers to something on line 920. So I'd like to find a way of inserting "line 920" into the footnote text. I'd even be happy to replace the footnote numbering with line numbering.
Any suggestions?
I have looked at /kb/getarticle.php?kb_id=59 (Get Line and Paragraph Number) for ideas, but am stumped - not knowing quite how to interpret that example.
macropod
05-13-2012, 05:03 PM
Hi dglp,
Try the following. If, having run the macro, you edit the document content and want to re-do the line referncing, simply re-run the macro.
Sub AddFootNoteLineRefs()
' Turn Off Screen Updating
Application.ScreenUpdating = False
' Define variables
Dim FtNt As Footnote, RngRef As Range, LineNum As Long
Dim RngNote As Range, StrPre As String
' Configure the prefix text for the line references
StrPre = "(Line: "
With ActiveDocument
' Initialize RngRef
Set RngRef = .Range(0, 0)
' Process all footnotes
For Each FtNt In .Footnotes
' Move RngRef until it meets the current footnote
Do While RngRef.End < FtNt.Reference.End
' Keep a line count
LineNum = LineNum + 1
Set RngRef = RngRef.GoTo(What:=wdGoToLine, Name:=LineNum)
Loop
' Point to the current footnote's content
Set RngNote = FtNt.Range
With RngNote
' Test for an existing line reference
If InStr(.Text, StrPre) = 1 Then
' Move the start forward one character,
' so we don't delete the footnote number
.Start = .Start + 1
' If found, delete everything before the trailing ')'
While .Characters.First <> ")"
.Words.First.Delete
Wend
' Delete the trailing ')'
.Characters.First.Delete
' Move the start forward one character
' so the new line reference goes where it should
.Start = .Start + 1
End If
' Add a new line reference
.InsertBefore StrPre & LineNum - 1 & ") "
End With
Next
End With
' Cleanup
Set RngRef = Nothing: Set RngNote = Nothing
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
Wow! Splendid!
Does the job - and makes my editing job much less fiddly!
Thank you!
I'm trying to understand what the code is doing - and it seems to me there's a lot happening in a short macro.
It looks like it's something like: find a footnote reference, find and copy the line number it's on, open the footnote for editing, delete number, add line number, go to the next footnote.
I'm supposing it could be useful to play with similar formats - using endnotes instead of footnotes, putting section or page numbers instead of lines, and so on. This macro looks like it provides a template for considering some similar tasks.
Thanks again!
macropod
05-14-2012, 03:20 AM
I'm trying to understand what the code is doing - and it seems to me there's a lot happening in a short macro.
It looks like it's something like: find a footnote reference, find and copy the line number it's on, open the footnote for editing, delete number, add line number, go to the next footnote.
In broad terms yes, but note that, unlike to link you referred to, nothing ever gets selected. The comments describe what's going on at each point. Everything inside the If ... End If block comes into play only if you've already generated footnotes beginning with '(Line: '. Otherwise it simply adds that, the line number and ') ' to the start of the footnote.
I'm supposing it could be useful to play with similar formats - using endnotes instead of footnotes, putting section or page numbers instead of lines, and so on. This macro looks like it provides a template for considering some similar tasks.
Yes, all of that is possible.
gmaxey
05-14-2012, 01:06 PM
Paul,
Nice work! I did notice that if the final line has a footnote then an endless loop can occur. I just tinkered a bit (could be a better way) and defined a terminal range to ensure an exit point:
Sub AddFootNoteLineRefs()
Dim oRngFinished As Word.Range
Dim oRngTerminal As Word.Range
Set oRngFinished = Selection.Range
Set oRngTerminal = ActiveDocument.Bookmarks("\EndOfDoc").Range
oRngTerminal.Select
Set oRngTerminal = ActiveDocument.Bookmarks("\Line").Range
'Turn Off Screen Updating
Application.ScreenUpdating = False
' Define variables
Dim FtNt As Footnote, RngRef As Range, LineNum As Long
Dim RngNote As Range, StrPre As String
' Configure the prefix text for the line references
StrPre = "(Line: "
With ActiveDocument
'Initialize RngRef
Set RngRef = .Range(0, 0)
'Process all footnotes
For Each FtNt In .Footnotes
'Move RngRef until it meets the current footnote
Do While RngRef.End < FtNt.Reference.End
' Keep a line count
LineNum = LineNum + 1
Set RngRef = RngRef.GoTo(What:=wdGoToLine, Name:=LineNum)
If FtNt.Reference.InRange(oRngTerminal) And RngRef.InRange(oRngTerminal) Then
LineNum = LineNum + 1
Exit Do
End If
Loop
' Point to the current footnote's content
Set RngNote = FtNt.Range
With RngNote
' Test for an existing line reference
If InStr(.Text, StrPre) = 1 Then
' Move the start forward one character,
' so we don't delete the footnote number
.Start = .Start + 1
' If found, delete everything before the trailing ')'
While .Characters.First <> ")"
.Words.First.Delete
Wend
' Delete the trailing ')'
.Characters.First.Delete
' Move the start forward one character
' so the new line reference goes where it should
.Start = .Start + 1
End If
' Add a new line reference
.InsertBefore StrPre & LineNum - 1 & ") "
End With
Next
End With
oRngFinished.Select
'Cleanup
Set RngRef = Nothing: Set RngNote = Nothing: Set oRngTerminal = Nothing: Set oRngFinished = Nothing
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
macropod
05-14-2012, 02:10 PM
Hi Greg,
Thanks for picking that up. Here, I think, is a simpler way -
• After 'With ActiveDocument' insert:
' Insert a temporary last paragraph (in case there's a footnote
' on the last line). This avoids getting stuck in an endless loop
.Range.InsertAfter vbCr
• After 'Next' insert:
' Remove the temporary last paragraph
.Range.Characters.Last.Delete
gmaxey
05-14-2012, 02:20 PM
Paul,
Sure that is simpler, but it doesn't look like you had to think about it as hard as I did ;-)
macropod
05-14-2012, 02:28 PM
Maybe, but why make work for yourself.
Actually, I did try a different approach, involving testing whether RngRef.End = .Range.End, but it never is. RngRef.End only ever gets to the start of a line. Extra code could be used to extend RngRef, but that's really taking the long way round.
The KISS principle wins!
Tinbendr
05-15-2012, 10:01 AM
WooYoo periscope down! Heehee:devil2: (I had my turn earlier on another board.)
pseudandry
03-06-2013, 06:16 PM
This was so cool! ...(and I did get stuck in the prior code's loop)...BUT... what if what I really want is to be able to use consecutive line numbers on all lines including footnotes - Word's default suppresses footnotes in "line numbers." When I'm editing, I must refer to line numbers regardless of their format - text, footnote, title, etc. :help Is there a way to un-suppress Word from ignoring consecutive line numbering the footnotes {double negative not intended}? If anyone can figure this out, I know you can :ipray: :wizard: [Then I'll have time to work on my emoticon fetish.]
Thank you! Pseudandry
Paul,
Nice work! I did notice that if the final line has a footnote then an endless loop can occur. I just tinkered a bit (could be a better way) and defined a terminal range to ensure an exit point:
Sub AddFootNoteLineRefs()
Dim oRngFinished As Word.Range
Dim oRngTerminal As Word.Range
Set oRngFinished = Selection.Range
Set oRngTerminal = ActiveDocument.Bookmarks("\EndOfDoc").Range
oRngTerminal.Select
Set oRngTerminal = ActiveDocument.Bookmarks("\Line").Range
'Turn Off Screen Updating
Application.ScreenUpdating = False
' Define variables
Dim FtNt As Footnote, RngRef As Range, LineNum As Long
Dim RngNote As Range, StrPre As String
' Configure the prefix text for the line references
StrPre = "(Line: "
With ActiveDocument
'Initialize RngRef
Set RngRef = .Range(0, 0)
'Process all footnotes
For Each FtNt In .Footnotes
'Move RngRef until it meets the current footnote
Do While RngRef.End < FtNt.Reference.End
' Keep a line count
LineNum = LineNum + 1
Set RngRef = RngRef.GoTo(What:=wdGoToLine, Name:=LineNum)
If FtNt.Reference.InRange(oRngTerminal) And RngRef.InRange(oRngTerminal) Then
LineNum = LineNum + 1
Exit Do
End If
Loop
' Point to the current footnote's content
Set RngNote = FtNt.Range
With RngNote
' Test for an existing line reference
If InStr(.Text, StrPre) = 1 Then
' Move the start forward one character,
' so we don't delete the footnote number
.Start = .Start + 1
' If found, delete everything before the trailing ')'
While .Characters.First <> ")"
.Words.First.Delete
Wend
' Delete the trailing ')'
.Characters.First.Delete
' Move the start forward one character
' so the new line reference goes where it should
.Start = .Start + 1
End If
' Add a new line reference
.InsertBefore StrPre & LineNum - 1 & ") "
End With
Next
End With
oRngFinished.Select
'Cleanup
Set RngRef = Nothing: Set RngNote = Nothing: Set oRngTerminal = Nothing: Set oRngFinished = Nothing
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
macropod
03-06-2013, 11:32 PM
The only way to do that would be to have a macro add a line number to every line in your document. There are numerous severe problems with that, including:
1. The line numbers become part of the text, making editing a nightmare
2. The line numbers change the formatting of the document
3. Change printers and the re-formatting Word does to reflect layour data returned by the printer driver can cause lines to wrap at different points, invalidating the bulk of the previous line numbers (by moving them away from the start/end of each line and even moving them to different lines).
PS: Please don't simply quote entire previous posts in your reply. If there is a need to reference something in a post, quote only that part. In this case, though, you didn't need to reference anything ...
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.