带有自定义标题的GetCrossReferenceItems



在我的Word文件中,我想把正文中的图形和附录中的图形区分开来,我把第二种图形称为"图a"。为了通过它们的ID正确地引用它们,我想创建一个包含所有自定义标题的数组。然而,GetCrossReferenceItems在这里似乎不起作用。如何正确地创建数组?

Sub Caption_Example()
CaptionLabels.Add Name:="Figure"
CaptionLabels.Add Name:="Figure A"
' Insert new figure caption
With Selection
.InsertCaption _
Label:="Figure", _
Title:=": A fancy title", _
Position:=wdCaptionPositionBelow, _
ExcludeLabel:=0
End With
' Insert a line break
Selection = vbCrLf
' Insert new figure A caption
With Selection
.InsertCaption _
Label:="Figure A", _
Title:=": Another fancy title", _
Position:=wdCaptionPositionBelow, _
ExcludeLabel:=0
End With

' Usually to get a list of Figures I would type:
x = (ActiveDocument.GetCrossReferenceItems(ReferenceType:="Figure"))
Debug.Print "First figure: "; x(1)
' But it doesn't work with figure A
'y = (ActiveDocument.GetCrossReferenceItems(ReferenceType:="Figure A"))  ' Doesnt work at all
y = (ActiveDocument.GetCrossReferenceItems(ReferenceType = "Figure A")) ' Work, shows everything, not only Figure A
' Insert a line break (doesn't work anymore)
Selection.Text = vbCrLf
' Yet, referring to it works
Selection.InsertCrossReference _
ReferenceType:="Figure A", _
ReferenceKind:=wdOnlyLabelAndNumber, _
ReferenceItem:="1", _
InsertAsHyperlink:=True, _
IncludePosition:=False, _
SeparateNumbers:=False, _
SeparatorString:=" "

End Sub

编辑:GetCrossReferenceItems似乎工作,如果我调用新的CaptionLabel "FigureA",因此,摆脱其名称中的空间。但是所有的数字都被称为数字。1等。

好了,我找到解决方案了。首先,你必须设置标题7看起来像"附录a"。(我使用标题7,因为我不使用这样的标题为我的正常文本。注意:"Heading"是用德语写的"Überschrift")。然后,下面的代码工作:

Sub solution()
' Make a level 7 heading
Selection.TypeText Text:="Some heading"
Selection.Style = ActiveDocument.Styles("Überschrift 7")
Selection.TypeParagraph
' Create captionlabel
CaptionLabels.Add Name:="FigureA"
With CaptionLabels("FigureA")
.NumberStyle = wdCaptionNumberStyleArabic
.IncludeChapterNumber = True  ' Include Chapter Nr., like A, B, ...
.ChapterStyleLevel = 7 ' Because my chapter level 7 are the Appendix headings.
.Separator = wdSeparatorPeriod
End With
' Insert new figureA caption
With Selection
.InsertCaption _
Label:="FigureA", _
Title:=": Another fancy title", _
Position:=wdCaptionPositionBelow, _
ExcludeLabel:=True   ' Should be true

End With

' Name it "Figure"
With Selection.Paragraphs(1).Range
.InsertBefore "Figure "
End With

' Refer to that figure
Selection.TypeParagraph
Selection.TypeText Text:="Refer to that by writing "
With Selection
.InsertCrossReference _
ReferenceType:="FigureA", _
ReferenceKind:=wdOnlyLabelAndNumber, _
ReferenceItem:=1, _
InsertAsHyperlink:=True, _
IncludePosition:=False, _
SeparateNumbers:=False, _
SeparatorString:=" "
End With
End Sub

编辑:问题似乎是"数字"之间的空格。和";A"在标题标签。

相关内容

  • 没有找到相关文章

最新更新