问题
I would like to detect and skip merged cells in vba on word 2010.
I'm making a macro which need to write in the first column but I've some merged cells and I don't want to write in. All examples I found was for Excel.
I tried to detect the number of columns per rows but that didn't works. I got "Error 5991 table has vertically merged cells"
.
So how can I just skip the row when I get merged cells ?
Sub test()
Dim Ro As Integer, Col As Integer
'init
Count = 1
Col = 1
For Ro = 4 To ActiveDocument.Tables(4).Rows.Count
'format
If Count < 10 Then
flag = "0"
Else
flag = ""
End If
'detect merge
If ActiveDocument.Tables(4).Rows(Ro).Cells.Count = 9 Then
ActiveDocument.Tables(4).Cell(Ro, Col).Range.Text = "R" & flag & CStr(Count)
Count = Count + 1
Else
Ro = Ro + 1
End If
Next Ro
End Sub
回答1:
i found a solution !
i used this to bypass the error:
On Error GoTo ErrorHandler
ErrorHandler: If Err.Number = 5991 Or Err.Number = 5941 Then Err.Clear Resume byebye End If
For Ro = 4 To ActiveDocument.Tables(4).Rows.Count
[My ugly code]
#in my case i can use a cell to determinate if it's a my row is merged or not
If Len(ActiveDocument.Tables(4).Cell(line, 5).Range.Text) > 0 Then
[My Ugly Code]
byebye:
Next Ro
Hope my solution can help you...
来源:https://stackoverflow.com/questions/28559703/how-to-detect-merged-cells-in-vba-word-2010