Check for merged cells with VBA

Hi

 

I'm just wondering how I would go about checking if a table in word has merged cells using VBA?

...and also, if possible, somehow note which table has merged cells

I'm thinking for something along the lines of:

 

sub tableformat

dim otbl as word.tables

for each otbl in selection.tables

     'something in here along the lines of: if otbl contains merged cells then

          'tablenumber = X

           'msgbox "Table "&tablenumber&" has merged cells."

           next otbl

     else

     'do stuff

next otbl

sub end

 

(All the tables are numbered but I'm not sure how to identify the bookmark of the table title in VBA code.)

 

Question Info


Last updated April 29, 2020 Views 9,261 Applies to:
Answer
Answer

For what it's worth, try this:

 

Sub tableformat()
    Dim i As Long
    Dim oRow As Row
    Dim oCol As Column
    On Error GoTo ErrHandler
    For i = 1 To Selection.Tables.Count
        For Each oRow In Selection.Tables(i).Rows
        Next oRow
NextStep:
        For Each oCol In Selection.Tables(i).Columns
        Next oCol
NextTable:
    Next i
    Exit Sub
ErrHandler:
    Select Case Err
        Case 5991
            MsgBox "Table #" & i & " has vertically merged cells"
            Resume NextStep
        Case Else
            MsgBox "Error " & Err.Number & ": " & _
                Err.Description & " in table #" & i
            Resume NextTable
    End Select
End Sub

---
Kind regards, HansV
https://www.eileenslounge.com

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

Answer
Answer
Once two (or more) cells have been merged, they form one cell. There is no property that specifies that the cell is the result of merging. So there is no really good way to do what you want. You might count the number of cells per row and per column, annd compare the width and height of cells, but that's tedious and not 100% conclusive.
---
Kind regards, HansV
https://www.eileenslounge.com

1 person was helped by this reply

·

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.