How to detect if a paragraph in MS Word 2007 has wrapped

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,911
I am automating name tag creation in MS Word 2007. Inserting First Name on line 1, Last Name on line 2 and Billet on line 3. Each line has a default font size. I am looking for a way to detect if any of the lines is long enough to wrap (which would push the bottom line out of the label) and then reduce the font size for that line enough so that the wrap is gone. The WordWrap method will show if a line is set to wrap, but I cannot figure out how to deterimine if a line has wrapped.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If your name tag text is in table cells, the following code will provide the number of lines in each cell...adjust to suit:
Code:
Sub GetCellLineCount()    Dim tblOne As Table
    Dim celTable As Cell
    Dim rngTable As Range


    Set tblOne = ActiveDocument.Tables(1)
    For Each celTable In tblOne.Range.Cells
        Set rngTable = celTable.Range
        rngTable.MoveEnd Unit:=wdCharacter, Count:=-1
        MsgBox rngTable.ComputeStatistics(wdStatisticLines)
    Next celTable
End Sub
The method "ComputeStatistics(wdStatisticLines)" can only be used with a range. If you have more than one table (likely) you can put this in a loop, and index using the table count as the upper limit.
Hope that helps,
 
Upvote 0
Assuming each 'line' is its own paragraph, try something based on:
Code:
Sub Demo()
With Selection.Paragraphs(1).Range
  Do While .Characters.First.Information(wdVerticalPositionRelativeToTextBoundary) <> _
    .Characters.Last.Information(wdVerticalPositionRelativeToTextBoundary)
    If .Font.Size = 1 Then Exit Do
    .Font.Size = .Font.Size - 0.5
  Loop
End With
End Sub
Note: If you use paragraph justification and the compatibility option to justify text the way WordPerfect 6.x does, you can often get more text on the line without reducing the point size.
 
Last edited:
Upvote 0
Thank you both for your replies. I was fairly sure the object model contained the answer somewhere, but did not know where to look.
 
Upvote 0

Forum statistics

Threads
1,215,949
Messages
6,127,877
Members
449,410
Latest member
adunn_23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top