Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-17-2023, 07:39 AM
eduzs eduzs is offline Count lines within selected text range Windows 10 Count lines within selected text range Office 2019
Expert
Count lines within selected text range
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default Count lines within selected text range

How to reliable count lines within selected text range with VBA?


oRng.ComputeStatistics(wdStatisticLines) apparently it doesn't work when there is more than one paragraph or the selection comprises more than one page.
I found a solution to count lines of the entire document, but not for selected text.
Thanks
__________________
Backup your original file before doing any modification.
Reply With Quote
  #2  
Old 09-17-2023, 08:09 AM
gmaxey gmaxey is offline Count lines within selected text range Windows 10 Count lines within selected text range Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Perhaps I am missing something, but this seems to work just fine:
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = Selection.Range
  MsgBox oRng.ComputeStatistics(wdStatisticLines)
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-17-2023, 09:38 AM
eduzs eduzs is offline Count lines within selected text range Windows 10 Count lines within selected text range Office 2019
Expert
Count lines within selected text range
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Thanks! Most of the time it works well, but I don't know why changing the selection more to the beginning or end of a line at start of a selection that comprises 2 paragraphs, results is varying, sometimes correct, sometimes total-1 (incorrect lines number by 1).
Few times, selecting the last characters of the 1st line (less +-10 character) through part of the 2nd line return just 1 line in selection, not 2.
__________________
Backup your original file before doing any modification.

Last edited by eduzs; 09-18-2023 at 01:17 AM.
Reply With Quote
  #4  
Old 09-17-2023, 03:33 PM
eduzs eduzs is offline Count lines within selected text range Windows 10 Count lines within selected text range Office 2019
Expert
Count lines within selected text range
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

I found a workaround that seems to work for me, by extending the selection to start of the 1st line and to the end of the last line before retrieve the selection lines count.
Code:
With Selection
    .StartOf Unit:=wdLine, Extend:=wdExtend
    .EndOf Unit:=wdLine, Extend:=wdExtend
    iLins = .Range.ComputeStatistics(wdStatisticLines)
End With
__________________
Backup your original file before doing any modification.
Reply With Quote
  #5  
Old 09-17-2023, 05:22 PM
gmaxey gmaxey is offline Count lines within selected text range Windows 10 Count lines within selected text range Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I wasn't seeing your issue after several trial runs. Anyway, glad you have it resolved.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying a VBA code only on a selected text or range RobertDany Word VBA 2 10-09-2021 08:31 AM
Count lines within selected text range Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Count lines within selected text range How to count words in a text, arrange them in separate lines and put the grammar class ? Marcway Word 6 12-14-2018 04:25 PM
Distribute text in one cell across a range of cells (overcoming selection.range.cells.count bug) slaycock Word VBA 0 02-18-2017 07:00 AM
Count lines within selected text range Deleting lines above selected text Chayes Word VBA 7 11-03-2013 03:43 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:24 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft