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上述代码currentNum = oT.range.Information(wdCaptionNumber)提示:对象已被删除,请修改
时间: 2024-03-05 11:51:54 浏览: 32
这个错误可能是因为你没有正确地设置选区范围。你可以尝试将代码修改为以下内容:
```
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
oRng.Select ' 将选区范围设置为表格的范围
With oRng
.InsertCaption "表", ""
.InsertBefore Chr(13)
.SetRange .Start, .Start
' 获取当前表格的编号
Dim currentNum As String
currentNum = oRng.Information(wdCaptionNumber) ' 将获取编号的代码修改为 oRng.Information(wdCaptionNumber)
' 插入表格的交叉引用
Word.Selection.InsertCrossReference "表", wdOnlyLabelAndNumber, currentNum
.InsertBefore "详见"
End With
```
这里我们将选区范围设置为表格的范围,并将获取编号的代码修改为 `oRng.Information(wdCaptionNumber)`。这样你就可以正确地获取表格的编号并插入表格的交叉引用了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)