Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Counting lines of text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas - 17 Jan 2005 15:18 GMT
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

Signature

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)

Greg Maxey - 17 Jan 2005 16:05 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> 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
[quoted text clipped - 17 lines]
>
> Thomas
Helmut Weber - 17 Jan 2005 16:26 GMT
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/
Helmut Weber - 17 Jan 2005 16:55 GMT
<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/

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.