如何在单词vba

时间:2018-02-26 19:13:26

标签: vba word-vba

我想在表格中每行的单元格1和2中插入交叉引用。但是交叉引用只会进入单元格1.甚至单元格2交叉引用也会进入单元格1.以下是代码并查看附图,请提示解决方案。我试图将光标移动到单元格中,但这似乎不起作用。 enter image description here

Private Sub CmdGenerateTable_Click()
On Error GoTo ErrHandler
Dim objTable As Word.Table
Dim i As Integer, n As Integer, xRefs As Variant
Dim rng As Word.Range

Set rng = ActiveDocument.Bookmarks("HeadingsTable").Range

If rng.Tables.Count > 0 Then
    rng.Tables(1).Delete
End If

Application.ScreenUpdating = False

'Get the CrossReferenceItems collection
xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)

Set objTable = rng.Tables.Add(rng, UBound(xRefs) + 1, 5)

objTable.Borders.Enable = True

objTable.Cell(1, 1).Range.Text = "Heading #"
objTable.Cell(1, 2).Range.Text = "Heading Text"
objTable.Cell(1, 3).Range.Text = "reserved"
objTable.Cell(1, 4).Range.Text = "reserved"
objTable.Cell(1, 5).Range.Text = "reserved"

For i = 2 To UBound(xRefs) + 1       

''Trying to insert cross reference in first cell 
      objTable.Cell(i, 1).Range.Select
        Selection.InsertCrossReference ReferenceType:="Heading", ReferenceKind:= _
                                                        wdNumberRelativeContext, ReferenceItem:=i - 1, InsertAsHyperlink:=True, _
                                                        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "


''Trying to insert cross reference in second cell         
        objTable.Cell(i, 2).Range.Select
        Selection.InsertCrossReference ReferenceType:="Heading", ReferenceKind:= _
                                                        wdContentText, ReferenceItem:=i - 1, InsertAsHyperlink:=True, _
                                                        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "



Next

Application.ScreenUpdating = True

Exit Sub
ErrHandler:
    MsgBox ("Line number: " + Erl + ", Description: " + Err.Description + ", Error number: " + Err.Number)
End Sub

0 个答案:

没有答案