带有自定义标题的 GetCrossReferenceItems
GetCrossReferenceItems with custom captions
在我的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
编辑:如果我将新的 CaptionLabel 称为“FigureA”,GetCrossReferenceItems
似乎可以工作,因此,去掉其名称中的 space。但随后所有数字都称为 FigureA.1 等
好的,我找到了解决方案。首先,您必须将标题 7 设置为类似于“附录 A”。 (我使用标题 7,因为我的普通文本不使用此类标题。注意:“标题”是用德语“Ü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”之间的 space。
在我的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
编辑:如果我将新的 CaptionLabel 称为“FigureA”,GetCrossReferenceItems
似乎可以工作,因此,去掉其名称中的 space。但随后所有数字都称为 FigureA.1 等
好的,我找到了解决方案。首先,您必须将标题 7 设置为类似于“附录 A”。 (我使用标题 7,因为我的普通文本不使用此类标题。注意:“标题”是用德语“Ü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”之间的 space。