Word VBA:确实存在的表格单元格的错误"The requested member of the collection does not exist"



我有一个Word VBA脚本,它为当前选择添加了一些标题和表格。我现在正试图让它从下表中提取信息,并将其放在正确的标题下。最终目标是将信息从表格格式中删除,以便更好地导航,因为Word的大纲无法识别表格中的标题。

在得到运行时错误5941之前,我只将表内容放入字符串变量中:请求的集合成员不存在。调试器转到以下行:

strChildren = rngSource.Tables(1).Cell(Row:=2, Column:=4).Range.Text

该表的行数远远不止两行四列。为了确保集合的成员存在,我使用了另一个脚本来为我提供当前选择的行和列:

Sub CellRowColumn()
'For the current selection, shows a message box with the cell row and column.
With Selection.Cells(1)
        MsgBox ("Column = " & .ColumnIndex & vbCr & "Row = " & .RowIndex)
End With
End Sub

我在我想复制的单元格中运行了这个,它确实显示了行2&第4列。

这是我正在使用的代码:

Sub ElementHeadings()
'With the current selection, adds the headings for each element in the 
'Elements and Attribute List (Description, Parent(s), and Child(ren)) and
'a table for attributes, with 3 columns, headed "Attribute
'Name", "Attribute Required?" and "Attribute Content")
Dim rngSelection As Range
Dim rngTable As Range
Dim rngHeading As Range
Dim rngSource As Range
Dim strCaption As String
Dim lngCaptionLength As Long
Dim strDescr As String
Dim strParents As String
Dim strChildren As String
Dim strVol As String
Dim strUsedIn As String

Set rngSelection = Selection.Range
'msgBox (rngSelection.Text)

With rngSelection
    .InsertAfter ("Description")
    .InsertParagraphAfter
    .Expand unit:=wdParagraph
    .InsertAfter ("Parent(s)")
    .InsertParagraphAfter
    .Expand unit:=wdParagraph
    .InsertAfter ("Child(ren)")
    .InsertParagraphAfter
    .Expand unit:=wdParagraph
    .InsertParagraphAfter
    .InsertParagraphAfter
    Set rngTable = .Paragraphs(5).Range
    .InsertAfter ("Volume & Chapter")
    .InsertParagraphAfter
    .Expand unit:=wdParagraph
    .InsertAfter ("Used In")
    .Expand unit:=wdParagraph
    .Style = "Heading 4"
'MsgBox (rngSelection.Text)
End With
ActiveDocument.Tables.Add Range:=rngTable, NumRows:=3, NumColumns:=3
With rngTable
    .Tables(1).Cell(1, 1).Range.Text = "Attribute Name"
    .Tables(1).Cell(1, 2).Range.Text = "Attribute Required?"
    .Tables(1).Cell(1, 3).Range.Text = "Attribute Content"
    .Select
    GenericMacros.TableFormat
    .Move unit:=wdParagraph, Count:=-1
    .Select
End With
rngSelection.Select
Set rngHeading = Selection.GoTo(what:=wdGoToHeading, Which:=wdGoToPrevious)
rngHeading.Expand unit:=wdParagraph
'MsgBox (rngHeading.Text)
rngTable.Select
strCaption = rngHeading.Text
lngCaptionLength = Len(strCaption)
strCaption = Left(strCaption, lngCaptionLength - 1)
Selection.InsertCaption Label:=wdCaptionTable, Title:=". <" _
    & strCaption & "> Attribute Table"
rngSelection.Select
Set rngSource = Selection.GoTo(what:=wdGoToTable, Which:=wdGoToNext)
rngSource.Expand unit:=wdTable
strDescr = rngSource.Tables(1).Cell(Row:=2, Column:=2).Range.Text
strParents = rngSource.Tables(1).Cell(Row:=2, Column:=3).Range.Text
strChildren = rngSource.Tables(1).Cell(Row:=2, Column:=4).Range.Text
strVol = rngSource.Tables(1).Cell(Row:=2, Column:=8).Range.Text
strUsedIn = rngSource.Tables(1).Cell(Row:=2, Column:=9).Range.Text
MsgBox ("strDescr = " & strDescr & vbCr & "strParents = " & strParents & _
    vbCr & "strChildren =" & strChildren & vbCr & "str3001Vol = " _
    & str3001Vol & "strUsedIn = " & strUsedIn)
End Sub

(如果问题出在文档上,而不是我的代码上,这可能最终会成为一个超级用户问题,而不是堆栈溢出问题。以前,我在从表中复制和粘贴时遇到了问题(复制文本,但无法在上面粘贴),但现在这种情况已经不再发生了。因此,如果代码没有明显的问题,可能是文档损坏或其他Word怪异。)

更新:我的源区域包含我刚刚创建的表,而不是我想从中提取的表,所以我修复了正在创建rngSource的Selection.Goto。

很高兴您能够跟踪代码的故障位置。使用Selection对象往往是不可靠的,因为它可能不是您编写代码时假设的位置(或它所在的位置)。

尽可能使用Word的对象要好得多。例如,创建表时,对变量进行Dim,然后在创建表时为其赋值。这给了你一个"手柄",无论在它之前发生什么样的编辑,稍后都会发生:

Dim tbl as Word.Table
Set tbl = ActiveDocument.Tables.Add(Range:=rngTable, NumRows:=3, NumColumns:=3). 
tbl.Cell(1,1).Range.Text = "Attribute Name"
'and so on...

要拿起一张现有的桌子,你需要能够识别它。如果你确定位置,那么:

Set tbl = ActiveDocument.Tables([index value])

如果这是一种您设置并重复使用的"模板"类型的文档,您可以将表格添加为书签(选择表格并插入书签,或者单击第一个单元格并插入书签),然后:

Set tbl = ActiveDocument.Bookmarks("BookmarkName").Range.Tables(1)

类似地,你可以替换这个:

rngHeading.Expand unit:=wdParagraph

如果你想使用以下段落,请明确:

Dim para as Word.Paragraph
Set para = rngHeading.Paragraphs(1)

它还可以帮助您知道您可以将范围"折叠"到其起点或终点(类似于在选择时按箭头键)。如果你想添加一些东西,格式化它,然后添加其他应该有不同格式的东西,这很有用。。。(作为连续使用InsertAfter然后返回并以不同格式设置内容的替代方法)。

我得到了类似OP的东西,在运行以下代码后:

Dim tbl As Word.Table: Set tbl = doc.Tables(2)
MsgBox tbl.Cell(1, 1).Range.Text

它的工作原理是每个表中应该至少有一个单元格,确实注意到我也访问了错误的表;-)

所以,你可以先用它来确定。

相关内容

最新更新