PDA

View Full Version : VBA to handle merged cells



chrscote
08-01-2016, 07:39 AM
I have a table in Word that contains a cell that merges 3 columns. In my VBA code, I have the following code:


For each oTbl in ActiveDocumnet.Tables
For each rRow in oTbl.Rows
//set text alignment
next rRow
next oTbl


However, when it gets to the table with the merged columns, I receive the Run-time error '5991' Cannot access individual rows in this collection because the table has vertically merged cells.

Is it possible to handle this case?

Chris

gmaxey
08-01-2016, 03:17 PM
Use the cells collection:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Word.Table, oRow As Row, oCell As Cell
For Each oTbl In ActiveDocument.Tables
If oTbl.Uniform Then
For Each oRow In oTbl.Rows
oRow.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Next oRow
Else
For Each oCell In oTbl.Range.Cells
oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Next oCell
End If
Next oTbl
lbl_Exit:
Exit Sub
End Sub

chrscote
08-02-2016, 04:36 AM
Thank you, I will try that instead.