简体   繁体   English

VBA将参考页插入MS word endnote

[英]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. Word没有此功能,所以我认为它必须是一个宏。 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: 尝试以下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

An alternative might be to use PAGEREF fields and hide the endnote references, eg 另一种方法可能是使用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

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. 然而,OP认为这种方法可以用一种有用的方式进行修改,所以我没有删除。 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 "-". 我无法立即重新研究它,但有些可能性可能是用子弹(如OP所建议的)替换每个尾注标记,或者甚至可能是像空格或“ - ”那样简单的东西。

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. 在上面的例子中,请注意每个页面只有一个链接,可能需要格式化以显示它是一个链接,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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