Sub 插入表格题注和交叉引用() Dim oCL As CaptionLabel Dim oDoc As Document Dim oRng As range Dim oT As table Set oT = Selection.Tables(1) Set oDoc = Word.ActiveDocument Set oCL = Word.CaptionLabels.Add("表") With oCL .IncludeChapterNumber = True .NumberStyle = wdCaptionNumberStyleArabic '设置题注编号的章节样式为二级标题 .ChapterStyleLevel = 2 .Separator = wdSeparatorHyphen End With Set oRng = oT.range With oRng .InsertCaption "表", "" .InsertBefore Chr(13) .SetRange .Start, .Start arrItems = oDoc.GetCrossReferenceItems("表") Word.Selection.InsertCrossReference "表", wdOnlyLabelAndNumber, UBound(arrItems) .InsertBefore "详见" End With修改以上代码中添加的交叉引用的编号为当前选择表格的编号,而不是遍历全文后的编号
时间: 2024-02-10 13:09:14 浏览: 67
活用WORD题注和交叉引用功能
可以尝试修改以下代码:
```
Dim oCL As CaptionLabel
Dim oDoc As Document
Dim oRng As Range
Dim oT As Table
Set oT = Selection.Tables(1)
Set oDoc = Word.ActiveDocument
Set oCL = Word.CaptionLabels.Add("表")
With oCL
.IncludeChapterNumber = True
.NumberStyle = wdCaptionNumberStyleArabic
.ChapterStyleLevel = 2
.Separator = wdSeparatorHyphen
End With
Set oRng = oT.Range
With oRng
.InsertCaption "表", ""
.InsertBefore Chr(13)
.SetRange .Start, .Start
' 获取当前表格的编号
Dim currentNum As String
currentNum = oT.Range.Information(wdCaptionNumber)
' 插入表格的交叉引用
Word.Selection.InsertCrossReference "表", wdOnlyLabelAndNumber, currentNum
.InsertBefore "详见"
End With
```
在插入交叉引用时,使用了当前表格的编号,即 `oT.Range.Information(wdCaptionNumber)`,而不是遍历全文后的编号。这样可以确保交叉引用的编号与当前表格的编号一致。
阅读全文