WordWrap in Table Cell

J

Julie Mascara

I have a process where I run a mail merge to create pages
of labels. The labels are contained in a table. After
the merge is complete, I would like to run a VBA to tell
me if any of the text on the label did not fit and had to
be wrapped to the next line. I thought I could use the
following code to see if the vertical position of the 1st
character is different than the last; however, I must be
doing something wrong because the values for each cell in
the row are the same. Please help!!!!!! Julie

For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells

Set myRange = oCell.Range

begRange = myRange.Information
(wdVerticalPositionRelativeToPage)
myRange.MoveEnd unit:=wdCharacter, count:=-1
endRange = myRange.Information
(wdVerticalPositionRelativeToPage)
If (begRange <> endRange) Then
Debug.Print myRange.Text + " " + CStr
(begRange) + " " + CStr(endRange)
End If
Next oCell
Next oTable

OUTPUT - the third line is actually wrapped

2134BWHT 72 95
ADF2VHT 72 95
LONGNAME575AWTLONGNAME575AWHTTHEEND 72 95
575BWHT 72 95
562VHT 72 95
 
D

Doug Robbins - Word MVP

Hi Julie,

The following will tell you if the text in a cell occupies more than one
line

Dim myrange As Range
For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells

Set myrange = oCell.Range
myrange.End = myrange.End - 1

begRange = myrange.Information(wdVerticalPositionRelativeToPage)
myrange.Collapse wdCollapseEnd
endRange = myrange.Information(wdVerticalPositionRelativeToPage)
If (begRange <> endRange) Then
Set myrange = oCell.Range
myrange.End = myrange.End - 1
MsgBox myrange.Text + " " + CStr(begRange) + " " +
CStr(endRange)
End If
Next oCell
Next oTable

It would not tell you that third line in your OUTPUT example was wrapped.
If the "lines" are individual paragraphs, you could check on the vertical
position of the start of the paragraph range and of a range at the end of
the paragraph.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Julie Mascara

Doug,

Thanks for the quick reply. I can see a difference now
and may be able to get this code to work. As an
alternative, can you tell me if there is a way to get the
x and y extents of the cell and determine if the text
inside the cell is past that range?

I can't believe I am finding this so difficult, I really
just want to turn the cell red to flag the user that a bad
label will be printed.

Thanks Julie
 
D

Doug Robbins - Word MVP

Hi Julie,

Dim myrange As Range
For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells

Set myrange = oCell.Range
myrange.End = myrange.End - 1

begRange = myrange.Information(wdVerticalPositionRelativeToPage)
myrange.Collapse wdCollapseEnd
endRange = myrange.Information(wdVerticalPositionRelativeToPage)
MsgBox endRange - begRange
Next oCell
Next oTable

Will give you the height of the text "in" a cell, whether it all fits or
not. Fill a cell with text that just fits and run it to determine the
height of the text that will just fit and then use the code to compare the
height of the text in each cell with the height of the text that will just
fit.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

Mark Tangard

Julie,

There are really two levels of flagging this problem.
In one scenario you'd want to flag an address only if
the label really overflows. In another, you'd want to
flag any line that wraps, even if it didn't make the
label overflow. The latter is important if you want
labels that not only don't leak but also look passable.
(Ever try fitting "Rancho Santa Margarita CA 92688-0001"
across a standard label in 12-point Tahoma?? Bleah!)

That's the level we needed, so I wrote this little macro
some time back to green-highlight any lines that wrapped.
(And then another macro, assigned to a hotkey, to find
the next green highlight in a document. But that's
another post.)

Sub FlagTooLongLabelLines()
Dim t As Table, c As Cell, ro As Row
Dim i As Long, k As Long
Dim r As Range, r2 As Range, PgCnt As Long
For Each t In ActiveDocument.Tables
PgCnt = PgCnt + 1
StatusBar = "Searching Page " & PgCnt
For Each ro In t.Rows
For Each c In ro.Cells
For i = 1 To c.Range.Paragraphs.Count - 1
Set r = c.Range.Paragraphs(i).Range
r.Collapse wdCollapseStart
Set r2 = r.Paragraphs(1).Range
r2.Collapse wdCollapseEnd
r2.MoveEnd wdCharacter, -1
If r2.Information(wdFirstCharacterLineNumber) _
r.Information(wdFirstCharacterLineNumber) _
Then
k = k + 1
r.Paragraphs(1).Range.HighlightColorIndex = _
wdBrightGreen
End If
Next
Next
Next
Next
If k = 0 Then
MsgBox "None found."
Else
MsgBox "Flagged " & k & " lines."
End If
End Sub

Hope this helps a little.

--
Mark Tangard, Microsoft Word MVP
Please reply only to the newsgroup, not by private email.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters
 
J

Julie Mascara

Exactly what I was looking for - THANKS!!!!!
-----Original Message-----
Julie,

There are really two levels of flagging this problem.
In one scenario you'd want to flag an address only if
the label really overflows. In another, you'd want to
flag any line that wraps, even if it didn't make the
label overflow. The latter is important if you want
labels that not only don't leak but also look passable.
(Ever try fitting "Rancho Santa Margarita CA 92688-0001"
across a standard label in 12-point Tahoma?? Bleah!)

That's the level we needed, so I wrote this little macro
some time back to green-highlight any lines that wrapped.
(And then another macro, assigned to a hotkey, to find
the next green highlight in a document. But that's
another post.)

Sub FlagTooLongLabelLines()
Dim t As Table, c As Cell, ro As Row
Dim i As Long, k As Long
Dim r As Range, r2 As Range, PgCnt As Long
For Each t In ActiveDocument.Tables
PgCnt = PgCnt + 1
StatusBar = "Searching Page " & PgCnt
For Each ro In t.Rows
For Each c In ro.Cells
For i = 1 To c.Range.Paragraphs.Count - 1
Set r = c.Range.Paragraphs(i).Range
r.Collapse wdCollapseStart
Set r2 = r.Paragraphs(1).Range
r2.Collapse wdCollapseEnd
r2.MoveEnd wdCharacter, -1
If r2.Information(wdFirstCharacterLineNumber) _
Then
k = k + 1
r.Paragraphs(1).Range.HighlightColorIndex = _
wdBrightGreen
End If
Next
Next
Next
Next
If k = 0 Then
MsgBox "None found."
Else
MsgBox "Flagged " & k & " lines."
End If
End Sub

Hope this helps a little.

--
Mark Tangard, Microsoft Word MVP
Please reply only to the newsgroup, not by private email.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters




.
 

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