PDA

View Full Version : [SOLVED] Excel VBA : how to merge cells in winword table with for... next



justuptou
02-23-2017, 09:32 AM
Under Excel VBA operating, I have to merge the cells in winword table with For ... Next function.
but system error code was shown '5941' when run "Rng.Cells.Merge", I cannot fixed the problem,
Could anyone guide me to modify that, thank you very much.

Error Message :

Run-time error '5941':
The requested member of the collection does not exist.

Target : Cells(1,1 & 2,1).Merge, Cells(1,2 & 2,2).Merge, Cells(1,3 & 2,3).Merge &
Cells(1,5 & 2,5).Merge, Cells(1,6 & 2,6).Merge

Figure : Winword Table below


1,1
1,2
1,3
1,4
1,5
1,6


2,1
2,2
2,3
2,4
2,5
2,6



































Coding :


Sub mergeCellinTable()
Dim ObjWord
Set ObjWord = CreateObject("Word.Application")

Dim objDoc
Set objDoc = ObjWord.Documents.Add

ObjWord.Visible = True
objDoc.Activate

With ObjWord
Dim Table1

Set Table1 = objDoc.tables.Add(Range:=objDoc.ActiveWindow.Selection.Range, _
NumRows:=6, _
NumColumns:=6, _
DefaultTableBehavior:=wdWord8TableBehavior)

Dim Rng
For I = 1 To 6
If I <> 4 Then
With objDoc.tables(1)
Set Rng = .Cell(1, I).Range
Rng.End = .Cell(2, I).Range.End
Rng.Cells.Merge
Rng.Text = "Merged Column Cells"
End With
End If
Next I
End With
End Sub