我需要替换Word引文中的文本。由于引用是只读的,我需要删除旧的引用并在此之后创建一个新引用:
Set r = fld.Result
s = fld.Code.Text
fld.Cut
r.Fields.Add r, wdFieldCitation, s, False
问题在于我无法在r
(或fld.Result
)中修改文字,因此我需要使用新文本创建新的Range
变量,但我还没有找到任何方式来做到这一点。如何在VBA中创建给定Range
变量的可食用克隆?
UPD: 这是我为替换引用字段而编写的破碎代码
Sub UpdateCitations()
Dim fld As Field
Dim s As String
Dim r As Range
For Each fld In ActiveDocument.Fields
If fld.Type = wdFieldCitation Then
If fld.Result.Text = "[Please use macros to update citations]" Then
Dim citationId As Integer
citationId = FindCitationIndex(Split(fld.Code.Text, " ")(2))
Set r = fld.Result
' Here I need to edit the r.Text variable, but I get error 6124,
' because fld.Result.Text is read-only in citaion fields
' r.Text = "Citation changed"
s = fld.Code.Text
fld.Cut
r.Fields.Add r, wdFieldCitation, s, False
End If
End If
Next
End Sub
Function FindCitationIndex(ByVal tag As String) As Integer
Dim src As Source
Dim counter As Integer
counter = 1
For Each src In ActiveDocument.Bibliography.Sources
If src.tag = tag Then
FindCitationIndex = counter
End If
counter = counter + 1
Next
End Function
UPD2
我的任务是创建一个宏或参考书目样式,可以通过alhabetically或参考顺序进行排序。引用应该是数字的。
我已经设法创建了一个参考书目样式,它将按照我需要的方式对源进行排序,但我无法在XSLT中检索参考书目中的索引,因此我决定发送消息{{ 1}}然后用带宏的实际索引替换此消息,但我既不能编辑也不能创建新的引文字段。 我运行Office 2010。
答案 0 :(得分:0)
我同意@Mehow,遗漏了一些重要信息。但请尝试更改代码的最后一行:
r.Fields.Add r, , s, False
答案 1 :(得分:0)
您可以在单词的快速样式中设置所需的样式,并使用以下命令以编程方式将其应用于引用:
Sub FormatSelection()
Dim objWord As New Word.Application
Dim wordDoc As Word.Document
Set wordDoc = objWord.Documents.Open(MSWordPath) 'Replace it with the File Path
'The Selection could be made by the command wordDoc.Paragraphs(PositionReference).Range.Select or by any other select method
With objword.Selection
.Style = SourceDoc.Styles(ItemHeaderStyle)'Style you've created for this purpose
End with
End Sub