Discussion:
Strange problem with Table Borders
(too old to reply)
Mike Clayton
2009-07-30 17:10:01 UTC
Permalink
Hi All,

I'm currently writing a VBA macro for applying some formatting to table
borders and it all more or less works, but I've hit a bit of a strange
problem in Word 2003 that I can't seem to work out.

Basically, if you create a duplicate of an existing table by ctrl-dragging
the "Table Select" icon in the top left corner of a table, then the top
border of the new table and the bottom border of the original table seem to
be linked somehow. This means that any formatting you apply to the top row of
the duplicate table also applies to the bottom of the original table.

To reproduce:

+ Create a new blank document and insert a new table.
+ Click anywhere in the table to display the "Table Select" icon in the top
left corner of it.
+ Hold down the ctrl key and drag the Table Select icon to create a
duplicate table.
+ In the VBA "Immediate" window run the following line of code:

Word.Application.ActiveDocument.Tables(2).Cell(1,1).Borders(wdBordertop).Visible = False

The top border of the top left cell in the duplicate table disappears, but
the bottom border of the bottom left cell in the original table disappears as
well. The same thing happens if you format the duplicate table's top-left
cell border using "Format -> Borders and Shading..." and change the "Apply
to" dropdown to "Cell".

Does anyone know how to either stop this happening by "unlinking" the
tables, or detect whether a given table is "linked" to the bottom of another
one?

I've been bashing my head against this for a few hours now so any help would
be greatly appreciated.

Thanks,

Mike
Jean-Guy Marcil
2009-07-30 22:20:24 UTC
Permalink
Post by Mike Clayton
Hi All,
I'm currently writing a VBA macro for applying some formatting to table
borders and it all more or less works, but I've hit a bit of a strange
problem in Word 2003 that I can't seem to work out.
Basically, if you create a duplicate of an existing table by
ctrl-dragging
the "Table Select" icon in the top left corner of a table, then the top
border of the new table and the bottom border of the original table seem to
be linked somehow. This means that any formatting you apply to the top row of
the duplicate table also applies to the bottom of the original table.
+ Create a new blank document and insert a new table.
+ Click anywhere in the table to display the "Table Select" icon in the top
left corner of it.
+ Hold down the ctrl key and drag the Table Select icon to create a
duplicate table.
Word.Application.ActiveDocument.Tables(2).Cell(1,1).Borders(wdBordertop).Visible = False
The top border of the top left cell in the duplicate table
disappears, but
the bottom border of the bottom left cell in the original table disappears as
well. The same thing happens if you format the duplicate table's top-left
cell border using "Format -> Borders and Shading..." and change the "Apply
to" dropdown to "Cell".
Does anyone know how to either stop this happening by "unlinking" the
tables, or detect whether a given table is "linked" to the bottom of another
one?
I've been bashing my head against this for a few hours now so any help would
be greatly appreciated.
Never seen that before, but after looking at this for a few minutes...

The procedure you used to create a copy of the table is actually creating an
extension of the first table.
If you select the second table, and, in its table properties, remove the
text wrapping, it automatically rejoins its "mother".

However, if you use a regular copy/paste to create a duplicate table this
"problem" does not present itself.
Also, if you drag the duplicate onto a different page, this mother/child
type of link is not created.
--
______________________________
Jean-Guy Marcil
Montreal, Canada
Mike Clayton
2009-07-30 23:21:01 UTC
Permalink
Hi Jean-Guy,

Thanks for the reply.

Unfortunately I need to try and get my macros to format existing documents
which include tables that were created this way, although that might have to
go out of the window if I can't find a solution...

For what it's worth, I've found a bit more out about it. Apparently the icon
in the corner of the table is properly called the "Table move handle":

http://office.microsoft.com/en-us/word/HP030762141033.aspx

Copying a table this way also seems to put an invisible frame in the
document - you can see it in Print Layout view if you turn on "show text
boundaries" in "Tools -> Options -> View".

I'd would be good to be able to detect whether an artibrary table is linked
to another one like this - even just being able to show a messagebox to the
user to say it can't be formatted properly would be a step up from where I am
at the moment.

Anyway, thanks again for looking at this.

Mike
Post by Jean-Guy Marcil
Post by Mike Clayton
Hi All,
I'm currently writing a VBA macro for applying some formatting to table
borders and it all more or less works, but I've hit a bit of a strange
problem in Word 2003 that I can't seem to work out.
Basically, if you create a duplicate of an existing table by
ctrl-dragging
the "Table Select" icon in the top left corner of a table, then the top
border of the new table and the bottom border of the original table seem to
be linked somehow. This means that any formatting you apply to the top row of
the duplicate table also applies to the bottom of the original table.
+ Create a new blank document and insert a new table.
+ Click anywhere in the table to display the "Table Select" icon in the top
left corner of it.
+ Hold down the ctrl key and drag the Table Select icon to create a
duplicate table.
Word.Application.ActiveDocument.Tables(2).Cell(1,1).Borders(wdBordertop).Visible
= False
The top border of the top left cell in the duplicate table
disappears, but
the bottom border of the bottom left cell in the original table disappears as
well. The same thing happens if you format the duplicate table's top-left
cell border using "Format -> Borders and Shading..." and change the "Apply
to" dropdown to "Cell".
Does anyone know how to either stop this happening by "unlinking" the
tables, or detect whether a given table is "linked" to the bottom of another
one?
I've been bashing my head against this for a few hours now so any help would
be greatly appreciated.
Never seen that before, but after looking at this for a few minutes...
The procedure you used to create a copy of the table is actually creating an
extension of the first table.
If you select the second table, and, in its table properties, remove the
text wrapping, it automatically rejoins its "mother".
However, if you use a regular copy/paste to create a duplicate table this
"problem" does not present itself.
Also, if you drag the duplicate onto a different page, this mother/child
type of link is not created.
--
______________________________
Jean-Guy Marcil
Montreal, Canada
Jean-Guy Marcil
2009-07-31 04:03:37 UTC
Permalink
Post by Mike Clayton
Hi Jean-Guy,
Thanks for the reply.
Unfortunately I need to try and get my macros to format existing
documents which include tables that were created this way, although
that might have to go out of the window if I can't find a solution...
For what it's worth, I've found a bit more out about it. Apparently
the icon in the corner of the table is properly called the "Table
http://office.microsoft.com/en-us/word/HP030762141033.aspx
Copying a table this way also seems to put an invisible frame in the
document - you can see it in Print Layout view if you turn on "show
text boundaries" in "Tools -> Options -> View".
Actually, it is not so much a frame as much as it is the fact that it set to
have text wrapping around it.
If you select the table and set the text wrapping to None (Table properties)
the table behaves as a regular able, but it might rejoin the "mother" so to
speak, depending on where it is the document in relation to the source
table.
Post by Mike Clayton
I'd would be good to be able to detect whether an artibrary table is
linked to another one like this - even just being able to show a
messagebox to the user to say it can't be formatted properly would be
a step up from where I am at the moment.
I am not sure that there is a way to detect such table.
You could try removing the text wrapping and if the total number of tables
in the document decreases by one, it means the table has rejoined another
one.
Then you can undo the removal of the text wrapping.
--
______________________________
Jean-Guy Marcil
Montreal, Canada
Mike Clayton
2009-07-31 10:03:05 UTC
Permalink
Jean-Guy,

I've tried playing with the Text Wrapping property and I think I've got a
solution now.

It seems the problem occurs when two tables sit immediately next to each
other in the text flow, with no text, line breaks, page breaks, etc, between
them. Normally two tables join together if you delete the range between them,
but if one of them is floating (i.e. text wrapping) then they stay as two
distinct tables. That's when their borders seem to start misbehaving.

As there's no other text between the two tables it means their ranges are
touching so I *think* all you need to do is, for a given table, check whether
the range of any other table in the document starts or ends immediately next
to it. My code to test for this is below.

Thanks for the pointer to text wrapping property
(objTable.Rows.WrapAroundText) - I don't think I would have worked it out
without this.

Cheers,

Mike



Public Sub TestTable()
If (FindAdjoiningTable(ActiveDocument.Tables(1)) Is Nothing) Then
MsgBox "Table is linked."
End If
End Sub

''' <summary>
''' Checks whether the specified table has another table as an immediate
''' neighbour in the parent document.
''' </summary>
''' <returns>
''' Returns a reference to the adjoining table if one is found,
''' otherwise returns a null reference (i.e. Nothing).
''' </returns>
Public Function FindAdjoiningTable(objTable As Word.Table) As Word.Table
Dim objDocument As Word.Document
Dim objCompare As Word.Table
Dim lngTableStart As Long
Dim lngTableEnd As Long
' get the start and end of the parameter's range so
' we don't have to keep calculating them in the loop.
lngTableStart = objTable.Range.Start
lngTableEnd = lngTableStart + objTable.Range.Characters.Count
' loop through each table in the document comparing it to the
' parameter to see it it is an immediate neighbour.
Set objDocument = objTable.Parent
For Each objCompare In objDocument.Tables
If (objCompare.Range.Start + objCompare.Range.Characters.Count =
lngTableStart) Then
' this is a 'child' linked table - the parameter table is
' immediately before it in the document, with no text between
them.
Set FindAdjoiningTable = objCompare
Exit Function
ElseIf (objCompare.Range.Start = lngTableEnd) Then
' this is a 'parent' linked table - the parameter table is
' immediately after it in the document, with no text between them.
Set FindAdjoiningTable = objCompare
Exit Function
End If
Next objCompare
' we didn't find a match so this table doesn't have a neighbouring table
Set FindAdjoiningTable = Nothing
End Function

Public Sub ReproduceBorderProblem()
Dim objDocument As Word.Document
Dim objRange As Word.Range
Dim objOriginal As Word.Table
Dim objDuplicate As Word.Table
' create the new document
Set objDocument = Word.Application.Documents.Add
' create the first table and format borders so we can see them
Set objRange = objDocument.Range.Duplicate
objRange.Collapse wdCollapseEnd
Set objOriginal = objDocument.Tables.Add(objRange, 3, 4)
objOriginal.Style = "Table Grid"
objOriginal.ApplyStyleHeadingRows = True
objOriginal.ApplyStyleLastRow = False
objOriginal.ApplyStyleFirstColumn = True
objOriginal.ApplyStyleLastColumn = False
objOriginal.ApplyStyleRowBands = True
objOriginal.ApplyStyleColumnBands = False
' insert a line break so that the next table doesn't merge
Set objRange = objDocument.Range.Duplicate
objRange.Collapse WdCollapseDirection.wdCollapseEnd
objRange.Text = vbCr
' insert another table directly after the first
Set objRange = objDocument.Range.Duplicate
objRange.Collapse wdCollapseEnd
Set objDuplicate = objDocument.Tables.Add(objRange, 3, 4)
objDuplicate.Style = "Table Grid"
objDuplicate.ApplyStyleHeadingRows = True
objDuplicate.ApplyStyleLastRow = False
objDuplicate.ApplyStyleFirstColumn = True
objDuplicate.ApplyStyleLastColumn = False
objDuplicate.ApplyStyleRowBands = True
objDuplicate.ApplyStyleColumnBands = False
' move the duplicate so we can see it's separate from the original. this
' isn't necessary to reproduce the problem.
objDuplicate.Rows.VerticalPosition = 20
objDuplicate.Rows.HorizontalPosition = 20
' float the second table - this is the core of the problem. floated tables
' that are adjacent in the text flow don't merge, but do still seem to
share
' the border where they meet.
objDuplicate.Rows.WrapAroundText = True
' remove the line break - the tables won't merge now as the second one
is 'floating'
Set objRange = objOriginal.Range.Duplicate
objRange.Collapse wdCollapseEnd
objRange.MoveEnd wdCharacter, 1
objRange.Delete
' format some cell and row borders - the original table and the duplicate
' seem to be sharing their bottom and top borders respectively. changes to
' one of them automatically affects the other.
objOriginal.Cell(objOriginal.Rows.Count,
1).Borders(wdBorderBottom).Visible = False
objDuplicate.Cell(1, 2).Borders(wdBorderTop).LineWidth = wdLineWidth150pt
End Sub
Mike Clayton
2009-07-31 10:10:01 UTC
Permalink
Got my test result the wrong way round - it should be this ("If Not" instead
of "If"):

Public Sub TestTable()
If Not (FindAdjoiningTable(ActiveDocument.Tables(1)) Is Nothing) Then
MsgBox "Table is linked."
End If
End Sub
Post by Mike Clayton
Jean-Guy,
I've tried playing with the Text Wrapping property and I think I've got a
solution now.
It seems the problem occurs when two tables sit immediately next to each
other in the text flow, with no text, line breaks, page breaks, etc, between
them. Normally two tables join together if you delete the range between them,
but if one of them is floating (i.e. text wrapping) then they stay as two
distinct tables. That's when their borders seem to start misbehaving.
As there's no other text between the two tables it means their ranges are
touching so I *think* all you need to do is, for a given table, check whether
the range of any other table in the document starts or ends immediately next
to it. My code to test for this is below.
Thanks for the pointer to text wrapping property
(objTable.Rows.WrapAroundText) - I don't think I would have worked it out
without this.
Cheers,
Mike
Public Sub TestTable()
If (FindAdjoiningTable(ActiveDocument.Tables(1)) Is Nothing) Then
MsgBox "Table is linked."
End If
End Sub
''' <summary>
''' Checks whether the specified table has another table as an immediate
''' neighbour in the parent document.
''' </summary>
''' <returns>
''' Returns a reference to the adjoining table if one is found,
''' otherwise returns a null reference (i.e. Nothing).
''' </returns>
Public Function FindAdjoiningTable(objTable As Word.Table) As Word.Table
Dim objDocument As Word.Document
Dim objCompare As Word.Table
Dim lngTableStart As Long
Dim lngTableEnd As Long
' get the start and end of the parameter's range so
' we don't have to keep calculating them in the loop.
lngTableStart = objTable.Range.Start
lngTableEnd = lngTableStart + objTable.Range.Characters.Count
' loop through each table in the document comparing it to the
' parameter to see it it is an immediate neighbour.
Set objDocument = objTable.Parent
For Each objCompare In objDocument.Tables
If (objCompare.Range.Start + objCompare.Range.Characters.Count =
lngTableStart) Then
' this is a 'child' linked table - the parameter table is
' immediately before it in the document, with no text between
them.
Set FindAdjoiningTable = objCompare
Exit Function
ElseIf (objCompare.Range.Start = lngTableEnd) Then
' this is a 'parent' linked table - the parameter table is
' immediately after it in the document, with no text between them.
Set FindAdjoiningTable = objCompare
Exit Function
End If
Next objCompare
' we didn't find a match so this table doesn't have a neighbouring table
Set FindAdjoiningTable = Nothing
End Function
Public Sub ReproduceBorderProblem()
Dim objDocument As Word.Document
Dim objRange As Word.Range
Dim objOriginal As Word.Table
Dim objDuplicate As Word.Table
' create the new document
Set objDocument = Word.Application.Documents.Add
' create the first table and format borders so we can see them
Set objRange = objDocument.Range.Duplicate
objRange.Collapse wdCollapseEnd
Set objOriginal = objDocument.Tables.Add(objRange, 3, 4)
objOriginal.Style = "Table Grid"
objOriginal.ApplyStyleHeadingRows = True
objOriginal.ApplyStyleLastRow = False
objOriginal.ApplyStyleFirstColumn = True
objOriginal.ApplyStyleLastColumn = False
objOriginal.ApplyStyleRowBands = True
objOriginal.ApplyStyleColumnBands = False
' insert a line break so that the next table doesn't merge
Set objRange = objDocument.Range.Duplicate
objRange.Collapse WdCollapseDirection.wdCollapseEnd
objRange.Text = vbCr
' insert another table directly after the first
Set objRange = objDocument.Range.Duplicate
objRange.Collapse wdCollapseEnd
Set objDuplicate = objDocument.Tables.Add(objRange, 3, 4)
objDuplicate.Style = "Table Grid"
objDuplicate.ApplyStyleHeadingRows = True
objDuplicate.ApplyStyleLastRow = False
objDuplicate.ApplyStyleFirstColumn = True
objDuplicate.ApplyStyleLastColumn = False
objDuplicate.ApplyStyleRowBands = True
objDuplicate.ApplyStyleColumnBands = False
' move the duplicate so we can see it's separate from the original. this
' isn't necessary to reproduce the problem.
objDuplicate.Rows.VerticalPosition = 20
objDuplicate.Rows.HorizontalPosition = 20
' float the second table - this is the core of the problem. floated tables
' that are adjacent in the text flow don't merge, but do still seem to
share
' the border where they meet.
objDuplicate.Rows.WrapAroundText = True
' remove the line break - the tables won't merge now as the second one
is 'floating'
Set objRange = objOriginal.Range.Duplicate
objRange.Collapse wdCollapseEnd
objRange.MoveEnd wdCharacter, 1
objRange.Delete
' format some cell and row borders - the original table and the duplicate
' seem to be sharing their bottom and top borders respectively. changes to
' one of them automatically affects the other.
objOriginal.Cell(objOriginal.Rows.Count,
1).Borders(wdBorderBottom).Visible = False
objDuplicate.Cell(1, 2).Borders(wdBorderTop).LineWidth = wdLineWidth150pt
End Sub
Jean-Guy Marcil
2009-08-01 17:34:56 UTC
Permalink
Post by Mike Clayton
Jean-Guy,
I've tried playing with the Text Wrapping property and I think I've
got a solution now.
It seems the problem occurs when two tables sit immediately next to
each other in the text flow, with no text, line breaks, page breaks,
etc, between them. Normally two tables join together if you delete
the range between them, but if one of them is floating (i.e. text
wrapping) then they stay as two distinct tables. That's when their
borders seem to start misbehaving.
As there's no other text between the two tables it means their ranges
are touching so I *think* all you need to do is, for a given table,
check whether the range of any other table in the document starts or
ends immediately next to it. My code to test for this is below.
Thanks for the pointer to text wrapping property
(objTable.Rows.WrapAroundText) - I don't think I would have worked it
out without this.
Glad I could help.
--
______________________________
Jean-Guy Marcil
Montreal, Canada
Loading...