Determine if a paragraph has multiple lines

T

Tim

I need to determine if a paragraph consists of multiple lines.

The only method that comes to mind is to check the vertical position
of the start of the paragraph with the vertical position of end of the
paragraph using ".Information(wdVerticalPositionRelativeToPage)".

Anyone have a better suggestion?

Thanks in Advance.
 
D

Dave Lett

Hi Tim,

You might be able to use something like the following:

ActiveDocument.Paragraphs(1).Range.Select
With Dialogs(wdDialogToolsWordCount)
.Execute
MsgBox "This paragraph contains " & .Lines & " lines."
End With

HTH,
Dave
 
T

Tim

This function appears to work

Sub TestMultipleLineParagraph()
MsgBox IsMultipleLineParagraph(Para:=Selection.Paragraphs(1))
End Sub

Function IsMultipleLineParagraph(Para As Paragraph) As Boolean
If Para.Range.Characters(1).Information(wdVerticalPositionRelativeToPage) = _

Para.Range.Characters(Para.Range.Characters.Count).Information(wdVerticalPositionRelativeToPage) Then
IsMultipleLineParagraph = False
Else
IsMultipleLineParagraph = True
End If
End Function
 
H

Howard Kaikow

Does the following do the deed?

Public Sub WhichLineAmIIn()
' How do I determine if a paragraph consists of more than 1 line?

Dim blnEndOfDoc As Boolean
Dim lngEnd As Long
Dim lngStart As Long
Dim rngEndPara As Range
Dim rngStartPara As Range

Set rngStartPara = Selection.Paragraphs(1).Range
Set rngEndPara = rngStartPara.Duplicate
With rngStartPara
If .End = .Start Then
blnEndOfDoc = (.End = ActiveDocument.Content.End - 1)
Else
blnEndOfDoc = (.End = ActiveDocument.Content.End)
End If
.Collapse Direction:=wdCollapseStart
lngStart = .Information(wdFirstCharacterLineNumber)
End With
With rngEndPara
.Collapse Direction:=wdCollapseEnd
If Not blnEndOfDoc Then
.MoveEnd Unit:=wdCharacter, Count:=-1
End If
lngEnd = .Information(wdFirstCharacterLineNumber)
End With

Debug.Print "Paragraph consists of " & IIf(lngStart = lngEnd, "", "more
than ") & "1 line"

Select Case WordBasic.CmpBookmarks("\Line", "\Para")
Case 0, 10
Debug.Print "Insertion point is in last line of the paragraph"
Case 8
Debug.Print "Insertion point is in first line of the paragraph"
Case Else
Debug.Print "Insertion point is not in first or last line of the
paragraph"
End Select

Set rngEndPara = Nothing
Set rngStartPara = Nothing
End Sub
 
H

Helmut Weber

Hi Tim,

still another one.

Public Function MultiLine(oPrg As Paragraph) As Boolean
On Error GoTo weiter:
If oPrg.Range.Bookmarks("\line").Range = oPrg.Range Then
MultiLine = True
Else
weiter:
MultiLine = False
End If
End Function

Sub test8946()
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Range.Paragraphs
' oPrg.Range.Select ' for testing only
MsgBox MultiLine(oPrg)
Next
End Sub

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
T

Tim

Using the Function MultiLine I get an error on the
Para.Range.Bookmarks("\Line").Range line when
it is processing a single line paragraph.

I don't understand why.
 
H

Helmut Weber

Hi Tim,
what error?

By the way, utilizing bookmarks("\line").range
might not have been a good idea after all,
as it fails if there are empty lines,
and it fails on the last paragraph in a doc,
and maybe in some other cases, too.
Sorry.

As in the song by Mike Batt:
[...]
it seemed to be a good idea at the time.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
T

Tim

Helmut,

Thanks for the reply.
I will go back to the solution using
..Information(wdVerticalPositionRelativeToPage).
The initial testing looked good but it seemed to have
problems with Landscape pages.

Thanks Again,

Tim
 

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