Connecting Tech Pros Worldwide Forums | Help | Site Map

Word: Counting lines selected

Newbie
 
Join Date: Apr 2007
Posts: 18
#1: Aug 28 '07
I need to count the number of lines selected in a Word document.

How can I achieve this in VBA?

Thanks

B

kadghar's Avatar
Expert
 
Join Date: Apr 2007
Location: Mexico City
Posts: 1,155
#2: Aug 28 '07

re: Word: Counting lines selected


Something like this will count the line changes

Expand|Select|Wrap|Line Numbers
  1. Sub cntlines()
  2. Dim i As Integer
  3. Dim n As Integer
  4. i = 1
  5. Do
  6.     i = InStr(i, Selection, Chr(13)) + 1
  7.     If i = 1 Then Exit Do
  8.     n = n + 1
  9. Loop
  10. MsgBox n
  11. End Sub
hope that helps
Newbie
 
Join Date: Apr 2007
Posts: 18
#3: Aug 29 '07

re: Word: Counting lines selected


Thanks for the response.

The method you suggest works fine provided each line ends on Chr(13).

How can I count wrapped lines, counting each wrap as a new line?

B
hariharanmca's Avatar
Lives Here
 
Join Date: Dec 2006
Location: Banglore/India
Posts: 1,987
#4: Aug 29 '07

re: Word: Counting lines selected


Quote:

Originally Posted by EByker

Thanks for the response.

The method you suggest works fine provided each line ends on Chr(13).

How can I count wrapped lines, counting each wrap as a new line?

B

wrap means same line. If you count as new line (then the return Value will not be same for even single file). Warp is just for our visible
kadghar's Avatar
Expert
 
Join Date: Apr 2007
Location: Mexico City
Posts: 1,155
#5: Aug 29 '07

re: Word: Counting lines selected


Quote:

Originally Posted by EByker

Thanks for the response.

The method you suggest works fine provided each line ends on Chr(13).

How can I count wrapped lines, counting each wrap as a new line?

B

Yeah, i see what you mean, i did a little research, and only find a characters count method. anyway, I made this little code with selection ranges and moving the selection one line down. I think it works allright.

Expand|Select|Wrap|Line Numbers
  1. Sub CntLine()
  2. Dim Sel(1 To 2) As Double
  3. Dim i As Double
  4. Sel(1) = Selection.Start
  5. Sel(2) = Selection.End
  6. i = 1
  7. Selection.HomeKey 5
  8. Do
  9.     Selection.MoveDown 5, 1
  10.     If Selection.Start > Sel(2) Then Exit Do
  11.     i = i + 1
  12. Loop
  13. MsgBox i
  14. Selection.Start = Sel(1)
  15. Selection.End = Sel(2)
  16. End Sub
Newbie
 
Join Date: Apr 2007
Posts: 18
#6: Aug 30 '07

re: Word: Counting lines selected


Thanks a lot! Works fine, just what I was looking for.

Regards

B
Reply


Similar Visual Basic 4 / 5 / 6 bytes