Log in

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



justuptou
02-23-2017, 09:39 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.D
ocuments.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

p45cal
02-23-2017, 12:09 PM
Sub mergeCellinTable()
Dim ObjWord, objDoc, Table1

Set ObjWord = CreateObject("Word.Application")
Set objDoc = ObjWord.Documents.Add
ObjWord.Visible = True
Set Table1 = objDoc.tables.Add(Range:=objDoc.ActiveWindow.Selection.Range, NumRows:=6, NumColumns:=6, DefaultTableBehavior:=wdWord8TableBehavior)
With Table1
For I = 1 To 6
If I <> 4 Then
.Cell(1, I).Merge MergeTo:=.Cell(2, I)
.Cell(1, I).Range.Text = "Merged column cells"
End If
Next I
End With
End Sub

justuptou
02-24-2017, 04:09 AM
Thanks for your help, your coding is work. but the error code : 424 was shown when I add them into my master Function.

Error Coding : .cell(K, I).Merge MergeTo:=.cell(K + 1, I)
Error Code : Run error code 424 Object Required


Coding Summary:

Dim objWord As Object, objDoc As Object, objSelection As Object
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim Table1



Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\ABC.doc")
objWord.Visible = True
Set objSelection = objWord.Selection



With objDoc

TableNo = objDoc.tables.Count

If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"

ElseIf TableNo > 1 Then

TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")

End If

With .tables(TableNo)

For iRow = 0 To 10
If iRow < 10 Then

'========================================================================== ====
' Test Program : if TempData(1,4) = True ? if Yes, Cells(1,2).merge exclude Column 4
'========================================================================== ====
If TempData(iRow + 1, 4) = True Then

With Table1
For I = 1 To 6
If I <> 4 Then
K = (J - 1) * 2 + 1
.cell(K, I).Merge MergeTo:=.cell(K + 1, I)
.cell(K, I).Range.Text = "Merged column cells"
End If
Next I
End With
End If
'========================================================================== ====
' End of Test Program Photo attached ?
'========================================================================== ====
'
'========================================================================== ====
' Below Coding <ba ba ba ... > for
' General information and Provide one Row if TempData ( iRow, 4 ) = True ?
' Information : Photo on Cells (3, 2)
'========================================================================== ====
End If
Next iRow
End With
End With

p45cal
02-24-2017, 04:37 AM
With .tables(TableNo)
For iRow = 0 To 10
If iRow < 10 Then
'========================================================================== ====
' Test Program : if TempData(1,4) = True ? if Yes, Cells(1,2).merge exclude Column 4
'========================================================================== ====
If TempData(iRow + 1, 4) = True Then
For I = 1 To 6
If I <> 4 Then
K = (J - 1) * 2 + 1
.cell(K, I).Merge MergeTo:=.cell(K + 1, I)
.cell(K, I).Range.Text = "Merged column cells"
End If
Next I
End If
'========================================================================== ====
' End of Test Program Photo attached ?
'========================================================================== ====
'
'========================================================================== ====
' Below Coding <ba ba ba ... > for
' General information and Provide one Row if TempData ( iRow, 4 ) = True ?
' Information : Photo on Cells (3, 2)
'========================================================================== ====
End If
Next iRow
End With
…but what's the value of J in K = (J - 1) * 2 + 1

justuptou
02-24-2017, 05:11 AM
P45cal, J and K are integer (row number) and control the data copy to correct row in winword table.

Target :
Copy data(1) to first row
If data(1,4) is false then
Second row is no information
End if

Copy data(2) to third row
If data(2,4) is true then
Copy Data(2,4) to forth row
Fifth row is no information
End if

Copy data(3) to sixth row
Bababa

I m not familiar to use the excel source to control winword but I need to learn and understand it, thanks

p45cal
02-24-2017, 06:34 AM
Yes, but what is the VALUE of J at that point?
Step through the code with F8 on the keyboard, and when you get to that line, hover over J with your mouse; it may show its value (if it's a global variable) or, as I suspect, it may say =Empty or some such.

p45cal
02-25-2017, 01:05 PM
Did you solve this one?
(I see you've posted a new different thread since I replied.)

justuptou
02-25-2017, 07:05 PM
Solved, thank you very much.

The problem is variable of table name, I found it when I checked global variable in each step.

thank you.

P.S. but I m new bird, I don't know how to set "Solved" in this forum, thanks again.