Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
| Search | Today's Posts | Mark Forums Read
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 21st, 2013, 01:42 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:
Code:
Set MySection = ActiveDocument.Sections(s)
MySection.Range.Select
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?

Thanks,

Tony
  #2 (permalink)  
Old September 21st, 2013, 10:44 PM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
  #3 (permalink)  
Old September 22nd, 2013, 08:51 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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)

Tony


Similar Threads
Thread Thread Starter Forum Replies Last Post
Section Line Not to Print bharatsaboo Crystal Reports 2 November 4th, 2007 11:45 AM
Line Item Numbers needed Bob Hansen Access VBA 1 May 16th, 2006 02:21 PM
Counting number of spaces in a line Suomi Access VBA 3 September 9th, 2005 02:34 PM
[?] Line numbers in VBA Chantale Access VBA 6 November 3rd, 2004 07:17 AM
counting record numbers in a loop sniffer Classic ASP Basics 2 October 21st, 2004 11:49 AM





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