简体   繁体   中英

VBA to insert reference page into MS word endnote

Book endnotes often forgo superscript numbers for page numbers. Eg, instead of

Abe Lincoln was assassinated with a pistol.^33
                   :
33. A single-shot derringer pistol.

books by several authors write

Abe Lincoln was assassinated with a pistol.
                   :
Page 297. Abe Lincoln was shot single-shot derringer pistol.

Word doesn't have this feature, so I believe it would have to be a Macro. I came up with simple code below that loops through all of the endnotes and adds

"Page ???. "

before each endnote, but what does "???" need to be to correctly insert the page number in my manuscript that the citation's located on?

Sub RedefineExistingEndNotes()
    Dim fn As Endnote
    For Each fn In ActiveDocument.Endnotes
        fn.Range.Paragraphs(1).Range.Font.Reset
        fn.Range.Paragraphs(1).Range.Characters(1).InsertBefore "Page" & "???" & " - "
    Next fn
End Sub

Try the below VBA code:

Sub InsertPageNumberForEndnotes()

Dim endNoteCount As Integer
Dim curPageNumber As Integer

If ActiveDocument.Endnotes.Count > 0 Then

For endNoteCount = 1 To ActiveDocument.Endnotes.Count

Selection.GoTo What:=wdGoToEndnote, Which:=wdGoToAbsolute, Count:=endNoteCount
curPageNumber = Selection.Information(wdActiveEndPageNumber)
ActiveDocument.Endnotes(endNoteCount).Range.Select
ActiveDocument.Application.Selection.Collapse (WdCollapseDirection.wdCollapseStart)
ActiveDocument.Application.Selection.Paragraphs(1).Range.Characters(1).InsertBefore "Page " & CStr(curPageNumber) & " - "


Next
End If

End Sub

An alternative might be to use PAGEREF fields and hide the endnote references, eg

Sub modifyEndNotes()
Const bookmarkText As String = "endnote"
Dim en As Word.Endnote
Dim rng As Word.Range
For Each en In ActiveDocument.Endnotes
  en.Reference.Bookmarks.Add bookmarkText & en.Index
  en.Reference.Font.Hidden = True
  Set rng = en.Range
  rng.Paragraphs(1).Range.Font.Hidden = True
  rng.Collapse WdCollapseDirection.wdCollapseStart
  rng.Text = "Page . "
  rng.SetRange rng.End - 2, rng.End - 2
  rng.Fields.Add rng, WdFieldType.wdFieldEmpty, "PAGEREF " & bookmarkText & en.Index & " \h", False
  'if necessary...
  'rng.Fields.Update
  en.Range.Font.Hidden = False
Next
Set rng = Nothing
End Sub

For a second run, you'd need to remove and re-insert the text and fields you had added.

Unfortunately, a further look suggests that it would be difficult, if not impossible, to hide the endnote references (in the endnotes themselves) without hiding the paragraph marker at the end of the first endnote para, which means that all the endnotes will end up looking like a single messy note. So I deleted this Answer.

However, the OP thought the approach could be modified in a useful way so I have undeleted. I can't re-research it right away but some possibilities might be to replace every endnote mark by a bullet (as suggested by the OP) or perhaps even something as simple as a space or a "-".

For example, something like this (which also hides the references using a different technique)...

Sub modifyEndNotes2()
' this version also formats the endnotes under page headings
Const bookmarkText As String = "endnote"
Dim en As Word.Endnote
Dim f As Word.Field
Dim i As Integer
Dim rng As Word.Range
Dim strSavedPage As String
strSavedPage = ""
For Each en In ActiveDocument.Endnotes
  en.Reference.Bookmarks.Add bookmarkText & en.Index
  Set rng = en.Range
  rng.Collapse WdCollapseDirection.wdCollapseStart
  If CStr(en.Reference.Information(wdActiveEndPageNumber)) <> strSavedPage Then
    strSavedPage = CStr(en.Reference.Information(wdActiveEndPageNumber))
    rng.Text = "Page :-" & vbCr & " - "
    rng.SetRange rng.End - 6, rng.End - 6
    rng.Fields.Add rng, WdFieldType.wdFieldEmpty, "PAGEREF " & bookmarkText & en.Index & " \h", False
    rng.Collapse WdCollapseDirection.wdCollapseEnd
  Else
    rng.Text = "- "
  End If
Next
If ActiveDocument.Endnotes.Count > 1 Then
  ActiveDocument.Styles(wdStyleEndnoteReference).Font.Hidden = True
Else
  ActiveDocument.Styles(wdStyleEndnoteReference).Font.Hidden = False
End If
Set rng = Nothing
End Sub

In the above case, notice that there is only one link to each page, that formatting might be needed to make it obvious that it is a link, and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM