![]() |
#1
|
|||
|
|||
![]()
The title says it all, really.
I hope some helpful expert can come up with some code to determine the number of lines of text there are in a given table cell, referenced by tbl.Cell(r, c) where tbl is the table. |
#2
|
|||
|
|||
![]()
A bit cumbersome but may work for you:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim strText As String Dim lngIndex As Long Dim oRng As Range Dim oCol As New Collection Dim strPosit As String strText = ActiveDocument.Tables(1).Cell(1, 1).Range.Text For lngIndex = 1 To Len(strText) Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range.Characters(lngIndex) On Error Resume Next strPosit = CStr(oRng.Information(wdVerticalPositionRelativeToPage)) oCol.Add strPosit, strPosit Next MsgBox oCol.Count lbl_Exit: Exit Sub End Sub ... of course all of the text will have to be on the same page. |
#3
|
||||
|
||||
![]()
Another non-optimal method is to use the selection object. It has the benefit of working across page breaks.
Code:
Sub HowManyLines() Dim aRng As Range, i As Integer, aTbl As Table Set aTbl = ActiveDocument.Tables(1) Set aRng = aTbl.Cell(1, 2).Range aRng.Select Selection.Collapse Direction:=wdCollapseStart Do While Selection.Range.End < aRng.End i = i + 1 Selection.MoveDown Unit:=wdLine, Count:=1 Loop MsgBox "Cell line count: " & i End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia Last edited by Guessed; Yesterday at 03:11 PM. Reason: Modified to use Table.Cell(r,c) as OP requested |
#4
|
||||
|
||||
![]()
Similarly:
Code:
Dim p As Single, l As Long, Rng As Range Set Rng = Tbl.Cell(r, c).Range With Rng .End = .End - 1 p = .Characters.First.Information(wdVerticalPositionRelativeToPage): l = 1 Do While .Words.First.Start < .Words.Last.Start .MoveStart wdWord, 1 If .Characters.First.Information(wdVerticalPositionRelativeToPage) < p Then p = .Characters.First.Information(wdVerticalPositionRelativeToPage): l = l + 1 End If Loop End With MsgBox l
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Thanks for the ideas. Maybe I should have mentioned that no cells involved will ever span a page so that's not an issue.
However, I have to report that only the code supplied by Guessed gives the right answer (at least in my environment) . The other two always come up with 1 line no matter how many there are (there will be from 1 to 5 in practice). |
#6
|
||||
|
||||
![]()
In my testing, the code I posted easily counts to more than 50 lines, including when page breaks are spanned.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to write a macro to count the number of lines between the start of a Word document to where the | soroush.kalantari | Word VBA | 3 | 08-08-2021 09:23 PM |
![]() |
Kubi | Excel | 4 | 08-24-2017 05:53 PM |
![]() |
alexolcz | Word VBA | 2 | 12-10-2015 09:54 PM |
How can I delete spaces & lines in a table cell | mrayncrental | Word VBA | 3 | 10-20-2014 07:09 PM |
Text Wrapping on Fixed Lines in a Form field/Table cell | okrmjr | Word Tables | 0 | 10-30-2009 08:52 AM |