Determine line number in word 2003 document

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Determine line number in word 2003 document

Post by ChrisGreaves »

The exercise is to determine whether a range of text fits on to a single line in the document (and if not, then reduce the style font size until it DOES fit on one line).

As far as I can see "selection.Information(wdFirstCharacterLineNumber)" will tell me the line number at the start of a selection (or range), but there is no "selection.Information(wdLastCharacterLineNumber)".
To determine the line number at the end of the selection, I would fabricate a range for the character beyond my selection and test that for range.Information(wdFirstCharacterLineNumber) and then compare or subtract the two. :scratch:
He who plants a seed, plants life.

User avatar
jscher2000
2StarLounger
Posts: 148
Joined: 26 Dec 2010, 18:17

Re: Determine line number in word 2003 document

Post by jscher2000 »

Yes. Similar idea:

Code: Select all

Function ParaHasMultipleLines(par As Word.Paragraph) As Boolean
Dim rngEnd As Word.Range
Set rngEnd = ActiveDocument.Range(Start:=par.Range.End - 1, End:=par.Range.End - 1)
If par.Range.Information(wdFirstCharacterLineNumber) <> _
        rngEnd.Information(wdFirstCharacterLineNumber) Then
    ParaHasMultipleLines = True
End If
Set rngEnd = Nothing
End Function

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Determine line number in word 2003 document

Post by ChrisGreaves »

jscher2000 wrote:Yes. Similar idea:
Jefferson thanks for the quick European-style confirmation (I refer to the blue skiing jacket).
I am fascinated to see that you pass a paragraph.
I embarked with a Selection.Range then quickly dropped back to a Selection.Range.Paragraphs(1).Range which works for my limited application:

Code: Select all

Function lngLineEnd(ByVal rng As Range) As Long
    Dim rng2 As Range
    Set rng2 = rng
    rng2.Start = rng.End + 1
    lngLineEnd = rng2.Information(wdFirstCharacterLineNumber) - 1
'Sub TESTlngLineEnd()
'    MsgBox lngLineEnd(Selection.Range)
'End Sub
End Function
Attached: A module with three related procedures.
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.

User avatar
jscher2000
2StarLounger
Posts: 148
Joined: 26 Dec 2010, 18:17

Re: Determine line number in word 2003 document

Post by jscher2000 »

ChrisGreaves wrote:(I refer to the blue skiing jacket)
Actually it's a tight-fitting "rash guard" used to reduce sun exposure during snorkeling. I won't be posting photos of the stomach area of this garment. :grin:

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Determine line number in word 2003 document

Post by ChrisGreaves »

jscher2000 wrote: ... tight-fitting "rash guard" ...
Speaking of rash decisions, I posted my efforts as a technical paper, and http://www.chrisgreaves.com/Invitations/PseudoCode.htm has a scanned image of my pseudo code.

I realized that I wanted to
  • Bump up font size to fit the print area
  • Reduce font size so that the data lines (name, address, date) didn't overflow their line
  • Bump up paragraph spacing to fit the print area.
The code to do this with two styles (one for the three data lines and one for the boilerplate text) was quite interesting, too)

Results at the foot of the pseudo code page. Source Code (Word 2003 VBA) available on request.
The envelope should be a pushover.
He who plants a seed, plants life.