VBA将参考页插入MS word endnote

时间:2016-01-21 01:45:39

标签: vba ms-word

预订尾注经常放弃页码的上标数字。例如,而不是

Abe Lincoln was assassinated with a pistol.^33
                   :
33. A single-shot derringer pistol.
几位作者的书写了

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

Word没有此功能,所以我认为它必须是一个宏。我想出了下面的简单代码,循环遍历所有尾注并添加

"Page ???. "

在每个尾注之前,但是" ???"需要将我的稿件中的页码正确插入引文的位置?

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

2 个答案:

答案 0 :(得分:2)

尝试以下VBA代码:

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

答案 1 :(得分:1)

另一种方法可能是使用PAGEREF字段并隐藏尾注引用,例如

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

对于第二次运行,您需要删除并重新插入已添加的文本和字段。

不幸的是,进一步的观察表明,如果不是不可能的话,隐藏尾注(在尾注本身中)而不将段落标记隐藏在第一个尾注段的末尾是很困难的,这意味着所有的尾注最终会看起来像一个凌乱的笔记。所以我删除了这个答案。

然而,OP认为这种方法可以以有用的方式进行修改,因此我没有删除。我不能马上重新研究它,但是一些可能性可能用子弹替换每个尾注标记(如OP所建议的那样),或者甚至是像空间或“ - ”这样简单的东西。 ”。

例如,像这样的东西(它也使用不同的技术隐藏引用)......

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

在上述情况下,请注意每个页面只有一个链接,可能需要格式化才能明确表明它是一个链接,依此类推。