Counting lines of text

T

Thomas

I am trying to count the number of lines of text in a Word document.
The object is to move from the lines, all in one paragraph, which do not
end with a ^P and add a ^P at the end of each so that the text may be
cut and pasted into another application.

When I try to count the number of line with

ActiveDocument.PageSetup.LinesPage

It always returns the number of lines *possible* on the page, not the
number of lines actually occupied with text.

I want to count the lines and then move down one at a time, add the ^P
until the count is exhausted.

A way to do that would be appreciated as well as how to generically
count lines, for future reference.

Thanks.

Thomas

--

Three stages of truth for scientists:
(1) It's not true.
(2) If it is true, it's not very important.
(3) We knew it all along.
Leo Szilard, (1898-1964, Key figure in the Manhattan Project)
 
G

Greg Maxey

Thomas,

I can't say definatively, but until the introduction of Word2003, a "line"
of text in a multi-line paragraph was not an object that could be counted
and manipulated in the manner you desire.

Thanks to a couple of regular contributors to this site, I have recently
learned of a method to do this using some new objects included with
Word2003. They are Pages, Rectagles, and Lines.

If you have Word2003 and you text is on a page without headers and footers
try:

Sub IterateDocLines()
'Credit to Jezebel and JGM for my introduction to this method
Dim pPage As Word.Page
Dim pLine As Word.Line
Dim myRange As Range

For Each pPage In ActiveWindow.ActivePane.Pages
For Each pLine In pPage.Rectangles(1).Lines
If InStr(pLine.Range, Chr(13)) < 1 Then
Set myRange = pLine.Range
myRange.InsertAfter (Chr(13))
End If
Next
Next
End Sub
 
H

Helmut Weber

Hi Thomas,
ActiveDocument.BuiltInDocumentProperties("number of Lines")
returns the number of lines in a very simple doc. However, there is no
"line" in the Word object model, unless, as I've just learned from
Greg, you got a higher version.

For putting a paragraph mark (¶) at the end of each line in the doc,
that hasn't a paragraph mark, you don't need a counter anyway.

Nor do you need a counter for making each line in a paragraph
a single paragraph.

Nor do you need a counter for making each line in the selection
a single paragraph.

It seems, you don't need a counter at all.

Here comes one simple example for the first or the only
paragraph in the selection:

With Selection
.Paragraphs(1).Range.Select
.Collapse
.Bookmarks("\line").Range.Select
While .Bookmarks("\line").Range.Characters.Last <> vbCr
.Range.InsertAfter vbCr
.MoveDown
Wend
End With

Greetings from Bavaria, Germany

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

Helmut Weber

<gr>
problems, it seems, with autohyphenation.
Version 2, improved, hopefully:

With Selection
.Paragraphs(1).Range.Select
.Collapse
.Bookmarks("\line").Range.Select
While .Bookmarks("\line").Range.Characters.Last <> vbCr
.EndKey
.TypeText Text:=vbCr
Wend
End With

Greetings from Bavaria, Germany

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

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