Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Merged cells in macros

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
putilkin - 11 Oct 2006 07:27 GMT
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.).
Jean-Guy Marcil - 13 Oct 2006 07:48 GMT
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...

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

JCNZ - 21 Oct 2006 01:14 GMT
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

> 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.).
Jean-Guy Marcil - 21 Oct 2006 02:11 GMT
JCNZ was telling us:
JCNZ nous racontait que :

> If you just want to know if there are merged cells in a table, try
> this:
[quoted text clipped - 8 lines]
>    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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 21 Oct 2006 02:25 GMT
Then wouldn't this do to answer that question:

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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> JCNZ was telling us:
> JCNZ nous racontait que :
[quoted text clipped - 14 lines]
> That's good, except that if I have two merged cells and one cell
> split in two, I will get a false negative.
Jean-Guy Marcil - 21 Oct 2006 05:51 GMT
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..

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Shauna Kelly - 21 Oct 2006 06:14 GMT
Hi Jean-Guy

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

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
[quoted text clipped - 13 lines]
> The help should come right out and say that if there are merged or split
> cells the result will be false..
Jean-Guy Marcil - 21 Oct 2006 07:28 GMT
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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.