Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Tony Logan Tony Logan is offline
external usenet poster
 
Posts: 4
Default macro to detect merged table cells?

I'm wondering if there's a way to detect merged table cells.

As a test, I wrote the below code, wanting to examine each table in a
document, then examine each cell. If I found a merged cell, I wanted to
change the cell color to blue and display a message box stating "Merged
cell." If the cell was not a merged cell, I wanted to change the cell color
to red and display a message box stating "No merge detected."

However, the code errors at this line:
If oCell.Merge = True Then

The error indicates that Merge is not an optional argument.

I'm not sure if there's a way to detect merged cells in this manner. Here's
all the code:

Sub FindMergedCells()

Dim oTable As Table
Dim orow As Row
Dim i As Integer
Dim oCell As Cell

For Each oTable In ActiveDocument.Tables
For Each orow In oTable.Rows
For Each oCell In orow.Cells
If oCell.Merge = True Then ' ** code errors here **
oCell.Shading.BackgroundPatternColor = wdColorBlue
MsgBox "Merged cell."
Else
oCell.Shading.BackgroundPatternColor = wdColorRed
MsgBox "No merge detected."
Next oCell

Next orow
Next oTable

End Sub

  #2   Report Post  
Posted to microsoft.public.word.tables
Tony Logan Tony Logan is offline
external usenet poster
 
Posts: 4
Default macro to detect merged table cells?

Forgot to note that I'm using Word 2003.

"Tony Logan" wrote:

I'm wondering if there's a way to detect merged table cells.

As a test, I wrote the below code, wanting to examine each table in a
document, then examine each cell. If I found a merged cell, I wanted to
change the cell color to blue and display a message box stating "Merged
cell." If the cell was not a merged cell, I wanted to change the cell color
to red and display a message box stating "No merge detected."

However, the code errors at this line:
If oCell.Merge = True Then

The error indicates that Merge is not an optional argument.

I'm not sure if there's a way to detect merged cells in this manner. Here's
all the code:

Sub FindMergedCells()

Dim oTable As Table
Dim orow As Row
Dim i As Integer
Dim oCell As Cell

For Each oTable In ActiveDocument.Tables
For Each orow In oTable.Rows
For Each oCell In orow.Cells
If oCell.Merge = True Then ' ** code errors here **
oCell.Shading.BackgroundPatternColor = wdColorBlue
MsgBox "Merged cell."
Else
oCell.Shading.BackgroundPatternColor = wdColorRed
MsgBox "No merge detected."
Next oCell

Next orow
Next oTable

End Sub

  #3   Report Post  
Posted to microsoft.public.word.tables
Greg Maxey Greg Maxey is offline
external usenet poster
 
Posts: 171
Default macro to detect merged table cells?

Tony,

I think just determining if a table is uniform is all you can do.

e.g.,

MsgBox ActiveDocument.Uniform

See:
http://gregmaxey.mvps.org/Table_Cell_Data.htm
For to table information code.

Tony Logan wrote:
I'm wondering if there's a way to detect merged table cells.

As a test, I wrote the below code, wanting to examine each table in a
document, then examine each cell. If I found a merged cell, I wanted to
change the cell color to blue and display a message box stating "Merged
cell." If the cell was not a merged cell, I wanted to change the cell color
to red and display a message box stating "No merge detected."

However, the code errors at this line:
If oCell.Merge = True Then

The error indicates that Merge is not an optional argument.

I'm not sure if there's a way to detect merged cells in this manner. Here's
all the code:

Sub FindMergedCells()

Dim oTable As Table
Dim orow As Row
Dim i As Integer
Dim oCell As Cell

For Each oTable In ActiveDocument.Tables
For Each orow In oTable.Rows
For Each oCell In orow.Cells
If oCell.Merge = True Then ' ** code errors here **
oCell.Shading.BackgroundPatternColor = wdColorBlue
MsgBox "Merged cell."
Else
oCell.Shading.BackgroundPatternColor = wdColorRed
MsgBox "No merge detected."
Next oCell

Next orow
Next oTable

End Sub


  #4   Report Post  
Posted to microsoft.public.word.tables
Jezebel Jezebel is offline
external usenet poster
 
Posts: 1,384
Default macro to detect merged table cells?

The reason for the error is that Merge is a method, not a property: it's how
you merge cells, not how you report if they are merged.

The logic gets a bit hairy, but try iterating the cells and tracking the
..RowIndex .ColumnIndex properties. If the last .ColumnIndex value in the row
is less than the maximum, that row contains merged cells. Bear in mind that
the internal mechanism of Word tables has changed significantly at least
twice over the last few versions of Word (and still doesn't work properly).
Apart from merged cells, there's also the possibility of split cells.

Don't iterate the rows: if the table has vertically merged cells, this may
throw an error.

For each oCell in oTable.Cells
...

It's not obvious that the identity of the merged cells is significant
anyway. Try this: create a table with five columns and four rows. In row 1,
merge cells 1 and 2; in row 2 merge cells 2 and 3, row 3: 3 and 4, row 4: 4
and 5. Now re-align the cell boundaries. Do you have a 4 x 4 table, or a 5 x
4 table with a merged cell in each row?






"Tony Logan" wrote in message
...
I'm wondering if there's a way to detect merged table cells.

As a test, I wrote the below code, wanting to examine each table in a
document, then examine each cell. If I found a merged cell, I wanted to
change the cell color to blue and display a message box stating "Merged
cell." If the cell was not a merged cell, I wanted to change the cell
color
to red and display a message box stating "No merge detected."

However, the code errors at this line:
If oCell.Merge = True Then

The error indicates that Merge is not an optional argument.

I'm not sure if there's a way to detect merged cells in this manner.
Here's
all the code:

Sub FindMergedCells()

Dim oTable As Table
Dim orow As Row
Dim i As Integer
Dim oCell As Cell

For Each oTable In ActiveDocument.Tables
For Each orow In oTable.Rows
For Each oCell In orow.Cells
If oCell.Merge = True Then ' ** code errors here **
oCell.Shading.BackgroundPatternColor = wdColorBlue
MsgBox "Merged cell."
Else
oCell.Shading.BackgroundPatternColor = wdColorRed
MsgBox "No merge detected."
Next oCell

Next orow
Next oTable

End Sub



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
macro runs from keyboard, but not from macro button-why? BobW Microsoft Word Help 7 May 10th 06 11:33 PM
Merged cells make row disappear in table Rosanne Ramos Tables 3 April 24th 06 09:20 AM
Table in a Form HiDbLevel Tables 12 February 27th 06 12:59 PM
Select specific cells in table via macro Bill Sturdevant Microsoft Word Help 1 July 27th 05 03:01 PM
Adding columns in table with merged cells Dee Tables 1 March 19th 05 01:18 PM


All times are GMT +1. The time now is 04:29 AM.

Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Copyright 2004-2023 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"