Vba 用Word中的尾注文本替换引用
我在Word中有一个文本,其中包括尾注的参考(1,2,3…)。通过交叉引用创建了对同一尾注的多个引用(例如,有多个1) 我想用尾注文本替换所有引用Vba 用Word中的尾注文本替换引用,vba,reference,ms-word,Vba,Reference,Ms Word,我在Word中有一个文本,其中包括尾注的参考(1,2,3…)。通过交叉引用创建了对同一尾注的多个引用(例如,有多个1) 我想用尾注文本替换所有引用 Sub endnotes2() Dim Note As Endnote Dim NoteReference As String Dim NoteText As String For Each Note In ActiveDocument.Endnotes With Note NoteText = .Range.T
Sub endnotes2()
Dim Note As Endnote
Dim NoteReference As String
Dim NoteText As String
For Each Note In ActiveDocument.Endnotes
With Note
NoteText = .Range.Text
NoteReference = .Index
Call Selection.SetRange(.Reference.End, .Reference.End)
Selection.Font.Superscript = True
Selection.TypeText (NoteText)
Selection.Font.Superscript = False
End With
Next Note
Do While ActiveDocument.Endnotes.Count > 0
Call ActiveDocument.Endnotes(1).Delete
Loop
Selection.Find.ClearFormatting
Selection.Find.Font.Superscript = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Superscript = False
With Selection.Find
.Text = ""
.Replacement.Text = " (^&)" 'The ^& here refers to the "found text", so if we found "abc" we will replace it with "(abc)"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
在互联网上,我发现了一个代码可以做到这一点。问题是,如果对同一尾注有多个引用,则只有位置在文档中第一个的引用才会被替换为文本(例如,只有第一个1,其他1不会)
我需要帮助如何用适当的尾注文本替换所有引用
Sub endnotes2()
Dim Note As Endnote
Dim NoteReference As String
Dim NoteText As String
For Each Note In ActiveDocument.Endnotes
With Note
NoteText = .Range.Text
NoteReference = .Index
Call Selection.SetRange(.Reference.End, .Reference.End)
Selection.Font.Superscript = True
Selection.TypeText (NoteText)
Selection.Font.Superscript = False
End With
Next Note
Do While ActiveDocument.Endnotes.Count > 0
Call ActiveDocument.Endnotes(1).Delete
Loop
Selection.Find.ClearFormatting
Selection.Find.Font.Superscript = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Superscript = False
With Selection.Find
.Text = ""
.Replacement.Text = " (^&)" 'The ^& here refers to the "found text", so if we found "abc" we will replace it with "(abc)"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
以下内容在我的简单测试中适用 由于尾注的交叉引用默认情况下不使用上标,因此搜索上标不是可靠的标准。此外,其他东西也可以上标。Word使用
Ref
字段管理交叉引用,这些字段指的是使用插入交叉引用
命令时放在尾注引用处的书签
此类书签以\u Ref
开头,后跟一个较长的数字。尾注字段使用名称NoteRef
。因此,获取尾注引用的书签名称(可能有多个)是有意义的,检查它们是否使用\u Ref
模式命名,然后搜索文档以使用书签
为了“查找”字段代码,使用模式^d
。所以搜索词是that,后跟字段代码的名称(NoteRef)和书签名称。如果搜索成功,则删除字段代码,并在该位置写入尾注文本。然后,搜索将从这一点继续到文档的结尾
因此,代码循环遍历所有尾注,获取每个尾注的引用,获取其所有书签,循环书签,检查名称(如上所述),并搜索NoteRef字段(如上所述)
最后,原始尾注引用将替换为尾注文本
Sub WriteEndNoteToAllEndNoteRefs()
Dim sEndNoteText As String
Dim rngEndNoteRef As Word.Range, rngSearch As Word.Range
Dim doc As Word.Document
Dim en As Word.Endnote
Dim bkm As Word.Bookmark
Dim bFound As Boolean
Set doc = ActiveDocument
For Each en In doc.Endnotes
Set rngEndNoteRef = en.Reference
sEndNoteText = en.Range.Text
For Each bkm In rngEndNoteRef.Bookmarks
If Left(bkm.Name, 4) = "_Ref" Then
Set rngSearch = doc.content
rngSearch.TextRetrievalMode.IncludeFieldCodes = True
Do
With rngSearch.Find
.Text = "^d NoteRef " & bkm.Name
.wrap = wdFindStop
bFound = .Execute
If bFound Then
rngSearch.Fields(1).Delete
rngSearch.Text = sEndNoteText
rngSearch.End = doc.content.End
End If
End With
Loop While bFound
End If
Next
rngEndNoteRef = sEndNoteText
Next
End Sub
请解释如何多次将脚注引用插入同一脚注。据我所知,这是不可能的方式,使他们“真实”的脚注(脚注的一部分,甚至尾注)集合,在代码中使用你显示。对不起,我的错误。我说的是脚注,而word文档包含尾注。通过交叉引用创建对同一尾注的多个引用。