p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Word VBA (http://p2p.wrox.com/forumdisplay.php?f=231)
-   -   Counting line numbers in a section (http://p2p.wrox.com/showthread.php?t=91067)

tfurnivall September 21st, 2013 01:42 PM

Counting line numbers in a section
I'm trying to get the number of lines in a section. I'd rather not loop through the lines if at all possible. So I tried using the following Code snippet:

Set MySection = ActiveDocument.Sections(s)
Debug.Print "Section"; s; " starts in line"; Selection.Information(wdFirstCharacterLineNumber)

I'm passing in a section number (s) to this procedure. Unfortunately the method returns only the line number within the section of the first character in the selection. Since I'm selecting the whole range, this will always be 1. (I suppose it could have been 0, but ....nah, don't go there ;-)

Has anybody else solved this problem?



Shasur September 21st, 2013 10:44 PM

Hi Tony

Do you want to get the number of lines in each section?

You can try using the Selection.Range.Start and Selection.Range.End to know the start and end positions in each Section

This can help you find the no of lines


tfurnivall September 22nd, 2013 08:51 AM

Line count in section
Thanks for the pointer Shasur,

The Start and End position are character positions (within the document, to be sure), but there is no easy way to turn them into line numbers. At the moment I'm stuck with building a separate table to track line numbers, and then iterating line-by-line over each section in turn (Yecch).

Does anyone have any (relatively) easy way to do as Shasur suggests, and convert character positions to line numbers? (Variable length lines, of course, along with Section Breaks)


All times are GMT -4. The time now is 10:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.