VBA to insert reference page into MS word endnote

前端 未结 2 739
时光说笑
时光说笑 2021-01-24 09:45

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

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


        
相关标签:
2条回答
  • 2021-01-24 10:18

    An alternative might be to use PAGEREF fields and hide the endnote references, e.g.

    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.

    0 讨论(0)
  • 2021-01-24 10:31

    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
    
    0 讨论(0)
提交回复
热议问题