![]() |
|
|
|
#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 to all contributors. All versions are now working for me.
My expertise is in Excel VBA and Word macros are another country. I have little idea of how these macros actually work, so can anyone advise on which is the most efficient? |
|
#6
|
|||
|
|||
|
Quote:
I would suggests using Paul's (Macropod) |
|
|
|
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 |