Merged cells in macros

P

putilkin

How can i find merged cells in word2003? How can i find if it is vertically
or horizont. merged? When word exports to html it specifies merged cells
correctly (rowspan or colspan) but nothing like that can be found in word
itself (in cell object etc.).
 
J

Jean-Guy Marcil

putilkin was telling us:
putilkin nous racontait que :
How can i find merged cells in word2003? How can i find if it is
vertically or horizont. merged? When word exports to html it
specifies merged cells correctly (rowspan or colspan) but nothing
like that can be found in word itself (in cell object etc.).

Working with merged cells in VBA is very arduous and unreliable.
You can detect if there are merged cells in a table with something like:

'_______________________________________
Dim lngRowID As Long
Dim lngColID As Long
Dim tblCurrent As Table
Dim boolNoError As Boolean

Set tblCurrent = Selection.Tables(1)

boolNoError = True

If Selection.Information(wdWithInTable) Then
With tblCurrent
On Error GoTo ErrHandle
lngRowID = Selection.Rows(1).Index
lngColID = Selection.Columns(1).Index
On Error GoTo 0
End With
Else
MsgBox "Place the cursor in a table."
End If

If boolNoError Then
MsgBox "There no merged cells in this table." _
, vbInformation, "No merged cells"
End If

Exit Sub

ErrHandle:
Select Case Err.Number
Case 5991
MsgBox "There are vertically merged cells in this table." _
, vbCritical, "Error"
boolNoError = False
Case 5992
MsgBox "There are horizontally merged cells in this table." _
, vbCritical, "Error"
boolNoError = False
Case Else
MsgBox Err.Description _
, vbCritical, "Error"
Err.Clear
Exit Sub
End Select

Err.Clear
Resume Next

End Sub
'_______________________________________

But as far as pinpointing which one... I guess you could iterate each cell,
check when you are on a new row, count the number of cells there were in the
previous row, if the number is smaller than the columns count, there were
horizontally merged cells. You have to constantly check if there is a next
cell on the same row and whether you re on the last table row.

You could repeat for the vertically merged cell by going down the columns.

But what if a cell is merged horizontally and vertically?

AS you can see, this would require lots of code and hours of testing. Maybe
someone already has such code and is willing to share.

Or, there may be some clever trick / little know method/property I am
unaware of...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

JCNZ

If you just want to know if there are merged cells in a table, try this:

Sub MergeYN()
With ActiveDocument.Tables(1)
If .Rows.Count * .Columns.Count <> .Range.Cells.Count Then
MsgBox "Merged cells."
Else
MsgBox "No merged cells."
End If
End With
End Sub
 
J

Jean-Guy Marcil

JCNZ was telling us:
JCNZ nous racontait que :
If you just want to know if there are merged cells in a table, try
this:

Sub MergeYN()
With ActiveDocument.Tables(1)
If .Rows.Count * .Columns.Count <> .Range.Cells.Count Then
MsgBox "Merged cells."
Else
MsgBox "No merged cells."
End If
End With
End Sub

That's good, except that if I have two merged cells and one cell split in
two, I will get a false negative.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Then wouldn't this do to answer that question:

Sub Test()
MsgBox Selection.Tables(1).Uniform
End Sub
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Then wouldn't this do to answer that question:

Sub Test()
MsgBox Selection.Tables(1).Uniform
End Sub

Yep, that would do it.

It is kind of misleading though because the help file says that this
property checks if all the rows have the same number of columns.. I first
thought that vertically merged cells would return True, but they don't.

The help should come right out and say that if there are merged or split
cells the result will be false..

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Shauna Kelly was telling us:
Shauna Kelly nous racontait que :
Hi Jean-Guy

I've recently had examples where .Uniform returns a false negative.
So I've taken to rolling my own.

Thanks for the info...

I did try to return a false positive or negative, but could not.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top