Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Requested Member does not exist on Merge of Word Table Cells

Status
Not open for further replies.

bubasti

Programmer
Aug 2, 2007
3
US
Hi Everyone,

I'm having issues merging some cells in a table that I created with VBA in a word document. It gives me the error "Run-time error '5941': The requested member of hte collection does not exist" even though I just wrote to the cells in the lines above.

Any suggestions??

Selection.GoTo What:=wdGoToBookmark, Name:="\EndOfDoc"

ActiveDocument.Range.Tables.Add Range:=Selection.Range, NumRows:=cntTotal, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitContent

With Selection.Tables(1)
.Range.Font.Name = "Arial"
.Range.Font.size = 10
.Rows.Height = 1

.Rows.Alignment = wdAlignRowCenter

.Columns(1).Width = 220
.Columns(2).Width = 220
.Columns(3).Width = 220
.Columns(4).Width = 75
.Cell(1, 1).Range.Bold = True
.Cell(1, 2).Range.Bold = True
.Cell(1, 3).Range.Bold = True
.Cell(1, 4).Range.Bold = True
.Cell(1, 1).Range.Text = "Requirement"
.Cell(1, 2).Range.Text = "Test Objective"
.Cell(1, 3).Range.Text = "Test Strategy/Approach"
.Cell(1, 4).Range.Text = "Documentation" & vbCrLf & "PR-" & txtDHFNum.Value & "-" & txtProtNum.Value

cnt = 2

For i = 1 To UBound(listReq)
.Cell(cnt, 1).Range.Text = listReq(i).reqNum & vbCrLf & listReq(i).reqDesc
For j = 1 To UBound(listReq(i).objList)
.Cell(cnt + j - 1, 2).Range.Text = listReq(i).reqNum & "." & j & " Verify that " & listReq(i).objList(j).objDesc
.Cell(cnt + j - 1, 3).Range.Text = listReq(i).objList(j).stratDesc
.Cell(cnt + j - 1, 4).Range.Text = listReq(i).objList(j).section
.Cell(1, 1).Merge MergeTo:=.Cell(2, 1)
Next
' HERE is the problem command
.Cell(1, 1).Merge MergeTo:=.Cell(2, 1)
' ****************************
cnt = cnt + UBound(listReq(i).objList)
Next

End With

Thanks!
 
Why have you already '.Cell(1, 1).Merge MergeTo:=.Cell(2, 1)' in the inner loop ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

I didn't have it in the loop before and it still gives me an error.

The following gives the same error "requested member does not exist"


Selection.GoTo What:=wdGoToBookmark, Name:="\EndOfDoc"

ActiveDocument.Range.Tables.Add Range:=Selection.Range, NumRows:=cntTotal, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitContent

With Selection.Tables(1)
.Range.Font.Name = "Arial"
.Range.Font.size = 10
.Rows.Height = 1

.Rows.Alignment = wdAlignRowCenter

.Columns(1).Width = 220
.Columns(2).Width = 220
.Columns(3).Width = 220
.Columns(4).Width = 75
.Cell(1, 1).Range.Bold = True
.Cell(1, 2).Range.Bold = True
.Cell(1, 3).Range.Bold = True
.Cell(1, 4).Range.Bold = True
.Cell(1, 1).Range.Text = "Requirement"
.Cell(1, 2).Range.Text = "Test Objective"
.Cell(1, 3).Range.Text = "Test Strategy/Approach"
.Cell(1, 4).Range.Text = "Documentation" & vbCrLf & "PR-" & txtDHFNum.Value & "-" & txtProtNum.Value

cnt = 2

For i = 1 To UBound(listReq)
.Cell(cnt, 1).Range.Text = listReq(i).reqNum & vbCrLf & listReq(i).reqDesc
For j = 1 To UBound(listReq(i).objList)
.Cell(cnt + j - 1, 2).Range.Text = listReq(i).reqNum & "." & j & " Verify that " & listReq(i).objList(j).objDesc
.Cell(cnt + j - 1, 3).Range.Text = listReq(i).objList(j).stratDesc
.Cell(cnt + j - 1, 4).Range.Text = listReq(i).objList(j).section
Next

cnt = cnt + UBound(listReq(i).objList)
Next
.Cell(1, 1).Merge MergeTo:=.Cell(2, 1)
End With
 
Hey PHV,

Nevermind. You were completely right. Thanks so much!
 
Hi bubasti. Could you please use the code tags when posting code? Thanks.

BTW:
Code:
Selection.GoTo What:=wdGoToBookmark, Name:="\EndOfDoc"
can be:
Code:
Selection.EndKey Unit:=wdStory
The only reason to be using EndOfDoc is if you are not in the MainStory.

and since you are bolding the entire row, doing it by Cell
Code:
        .Cell(1, 1).Range.Bold = True
        .Cell(1, 2).Range.Bold = True
        .Cell(1, 3).Range.Bold = True
        .Cell(1, 4).Range.Bold = True
would be easier using one instruction, for the Row:
Code:
   .Row(1).Range.Bold = True
In fact, it would be all round easier to make a table object, and not use Selection at all.
Code:
Dim r As Word.Range
Dim atable As Word.Table
Set r = ActiveDocument.Range
r.Collapse Direction:=wdCollapseEnd
[COLOR=red]' collapse Range to end of the document
' AND use it for the table insertion range parameter[/color red]
ActiveDocument.Range.Tables.Add Range:=[b]r[/b], _
   NumRows:=cntTotal, NumColumns:=4
[COLOR=red] ' set table object for the last one[/color red]
Set atable = ActiveDocument.Tables(ActiveDocument.Tables.Count)
With atable
   .Rows(1).Range.Bold = True
   With .Rows
      .Alignment = wdAlignRowCenter
      .Height = 1
   End With
   With .Range
      .Font.Name = "Arial"
      .Font.Size = 10
      .Cells(1).Range.Text = "Requirement"
      .Cells(2).Range.Text = "Test Objective"
      .Cells(3).Range.Text = "Test Strategy/Approach"
      .Cells(4).Range.Text = "Documentation" & vbCrLf & _
         "PR-" & txtDHFNum.Value & "-" & txtProtNum.Value
   End With
End With
You can put content into the cells by count (rather than by row/column), when using the range. This works as you are indeed putting your text into the first four cells.




faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top