给定编号项目的文本,我需要插入对特定编号项目(来自简单编号列表)的交叉引用。用户选择他们想要交叉引用的列表项以及插入交叉引用的位置,如下所示:
1. This is item 1
2. This is item 2 <-- user selects this line
3. This is item 3
------------------
[2] This is a reference to item 2 <-- user ctrl selects this line too
^ macro will insert the 2 as a cross reference to the selected list item
但是,可能有许多带有相同文本的编号项目,因此我需要一种方法来确定哪些项目是选定的项目。我计划选择具有正确文本的每个项目,检查它是否在文档中的正确位置,如果是,我现在知道它的“ReferenceItem:”编号,以便我可以插入交叉引用它。我遍历以下结果:
ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
获取带有正确文本的编号项目的参考编号,但我不知道如何选择每个候选人,以便测试其位置。就像是:
ActiveDocument.Goto.CrossReferenceItem(81)
这是在 Word 2010 中。
代码示例(已删除所有错误检查):
'Create a temporary character style
ActiveDocument.Styles.Add "_TEMP_STYLE_", wdStyleTypeCharacter
'Apply the temporary style to the discontiguous selection
Selection.Style = ActiveDocument.Styles("_TEMP_STYLE_")
Set olRange = ActiveDocument.Range
With olRange.Find
.ClearAllFuzzyOptions
.ClearFormatting
.ClearHitHighlight
.Style = ActiveDocument.Styles("_TEMP_STYLE_")
.Text = ""
.Wrap = wdFindStop
'llNumberedParaStart is used in the check of a numbered item to see if it is
'the right one
llNumberedParaStart = -1
llChildParaStart = -1
Do While .Execute
If .Found Then
If olRange.Paragraphs(1).Range.ListFormat.ListType = wdListSimpleNumbering Then
llNumberedParaStart = olRange.Characters(1).Start
' Do some work before this to get the text to check for
slNumberedParaText = Selection.Text
Else
llChildParaStart = olRange.Characters(1).Start
End If
Loop
End With
slaNumItems = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
'Loop through all the numbered items and find the correct one
For ilRefItem = 0 To UBound(slaNumItems)
If InStr(slNumberedParaText, slaNumItems(ilRefItem)) = 0 Then
'Check to make sure the reference is the correct one
********** select the numbered item and check its position **********
'Insert the cross reference after selecting the correct location
Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberRelativeContext, ReferenceItem:=ilRefItem, _
InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, _
SeparatorString:=" "
Exit For
End If
Next ilRefItem