![]() |
|
|
|
#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; 10-21-2025 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] |
|
#8
|
|||
|
|||
|
Paul, I tested with your code as well and was not getting proper results. Changing your
If .Characters.First.Information(wdVerticalPositionRe lativeToPage) < p Then To If .Characters.First.Information(wdVerticalPositionRe lativeToPage) <> p Then Resolved |
|
#9
|
||||
|
||||
|
Quote:
Code:
If .Characters.First.Information(wdVerticalPositionRelativeToPage) <> p Then Code:
If .Characters.First.Information(wdVerticalPositionRelativeToPage) > p ![]() Corrected.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#10
|
|||
|
|||
|
Thanks to all contributors. All versions are now working for me.
My expertise is in Excel VBA and Word macros are another country. With apologies to vivka, I have little idea of how these macros actually work, so can anyone advise on which is the most efficient? |
|
#11
|
|||
|
|||
|
Quote:
I would suggests using any working versions of Paul's (Macropod) |
|
#12
|
|||
|
|||
|
Paul, I have tested and rested with a simple document with one 2 x 2 table where the text in the target cell splits across a page. In every instance without using "<> p" I do not get the expected count.
In one test there were two lines of text on the first page and 3 lines on the second page using either "< p" or "> P" the return is 2. Only with using "<> p" do I get the expected return of 5. |
|
#13
|
||||
|
||||
|
Quote:
Code:
If .Characters.First.Information(wdVerticalPositionRelativeToPage) > p
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| 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 |
count the number of text in a cell based off a different cells text
|
Kubi | Excel | 4 | 08-24-2017 05:53 PM |
Row Number of Selected Cell in Table
|
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 |