A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

How many lines used in a table cell?



 
 
Thread Tools Display Modes
  #1  
Old February 2nd, 2006, 11:26 PM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Is there a simple way to tell from VBA code how many lines of text are in a
table cell?

Ed


  #2  
Old February 2nd, 2006, 11:34 PM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Not simple, no. One method is to iterate the characters of the text, and
check .Information(wdVerticalPositionRelativeToPage) for each: the number of
different values you get is the number of lines.





"Ed" wrote in message
...
Is there a simple way to tell from VBA code how many lines of text are in
a
table cell?

Ed




  #3  
Old February 3rd, 2006, 05:13 AM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Another way is to put the Selection at the beginning of the cell and
count how many times you can repeat the .MoveDown method until the
Selection either goes into the next row or drops out of the table.
Unfortunately, you have to use the Selection because the Range object
doesn't have a .MoveDown method.

Sub LinesInCell()
Dim nLines As Long, nCurrRow As Long
Dim rgSaveSel As Range
Set rgSaveSel = Selection.Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Not in a table"
Exit Sub
End If

With Selection
.Cells(1).Select
.Collapse wdCollapseStart
nCurrRow = .Information(wdEndOfRangeRowNumber)
nLines = 0

Do
nLines = nLines + 1
.MoveDown unit:=wdLine, Count:=1, Extend:=False
If Not .Information(wdWithInTable) Then Exit Do
Loop Until .Information(wdEndOfRangeRowNumber) nCurrRow
End With

MsgBox nLines & " line(s)"
rgSaveSel.Select
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Fri, 3 Feb 2006 09:34:37 +1100, "Jezebel"
wrote:

Not simple, no. One method is to iterate the characters of the text, and
check .Information(wdVerticalPositionRelativeToPage) for each: the number of
different values you get is the number of lines.


"Ed" wrote in message
...
Is there a simple way to tell from VBA code how many lines of text are in
a
table cell?

Ed

  #4  
Old February 3rd, 2006, 05:58 AM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

There might be a rigorous and elegant way to do it using something along the
lines of

activedocument.ActiveWindow.Panes(x).Pages(y).Rect angles(z).Lines(t).Rectangles.Count

But I have not the faintest idea how this bit of the object model is
supposed to work.





"Jay Freedman" wrote in message
...
Another way is to put the Selection at the beginning of the cell and
count how many times you can repeat the .MoveDown method until the
Selection either goes into the next row or drops out of the table.
Unfortunately, you have to use the Selection because the Range object
doesn't have a .MoveDown method.

Sub LinesInCell()
Dim nLines As Long, nCurrRow As Long
Dim rgSaveSel As Range
Set rgSaveSel = Selection.Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Not in a table"
Exit Sub
End If

With Selection
.Cells(1).Select
.Collapse wdCollapseStart
nCurrRow = .Information(wdEndOfRangeRowNumber)
nLines = 0

Do
nLines = nLines + 1
.MoveDown unit:=wdLine, Count:=1, Extend:=False
If Not .Information(wdWithInTable) Then Exit Do
Loop Until .Information(wdEndOfRangeRowNumber) nCurrRow
End With

MsgBox nLines & " line(s)"
rgSaveSel.Select
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Fri, 3 Feb 2006 09:34:37 +1100, "Jezebel"
wrote:

Not simple, no. One method is to iterate the characters of the text, and
check .Information(wdVerticalPositionRelativeToPage) for each: the number
of
different values you get is the number of lines.


"Ed" wrote in message
...
Is there a simple way to tell from VBA code how many lines of text are
in
a
table cell?

Ed



  #5  
Old February 3rd, 2006, 09:00 AM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Jezebel was telling us:
Jezebel nous racontait que :

There might be a rigorous and elegant way to do it using something
along the lines of

activedocument.ActiveWindow.Panes(x).Pages(y).Rect angles(z).Lines(t).Rectangles.Count

But I have not the faintest idea how this bit of the object model is
supposed to work.


I believe that the rectangle object usually refers to elements on the page,
such as header, footer, textboxes, main story, etc.

I do not think it is possible to use this to get the line count in a cell...
but it is 3:00 am and I might be totally wrong!


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site:
http://www.word.mvps.org


  #6  
Old February 3rd, 2006, 09:22 AM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

I've followed it far enough to establish that you can retrieve the
individual lines on the page; but quite how, I don'tknow.


"Jean-Guy Marcil" NoSpam@LeaveMeAlone wrote in message
...
Jezebel was telling us:
Jezebel nous racontait que :

There might be a rigorous and elegant way to do it using something
along the lines of

activedocument.ActiveWindow.Panes(x).Pages(y).Rect angles(z).Lines(t).Rectangles.Count

But I have not the faintest idea how this bit of the object model is
supposed to work.


I believe that the rectangle object usually refers to elements on the
page, such as header, footer, textboxes, main story, etc.

I do not think it is possible to use this to get the line count in a
cell... but it is 3:00 am and I might be totally wrong!


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site:
http://www.word.mvps.org



  #7  
Old February 3rd, 2006, 03:25 PM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Jezebel was telling us:
Jezebel nous racontait que :

I've followed it far enough to establish that you can retrieve the
individual lines on the page; but quite how, I don'tknow.


Something like this:

'_______________________________________
Dim i As Long
Dim strTargetLine As String

With ActiveWindow.Panes(1).Pages(1)
If .Rectangles.Count 1 Then
For i = 1 To .Rectangles.Count
If .Rectangles(i).RectangleType = wdTextRectangle Then _
If .Rectangles(i).Range.StoryType = wdMainTextStory Then
Exit For
Next
End If
strTargetLine = .Rectangles(i).Lines(5).Range.Text
End With
'_______________________________________

You have to check for the rectangle type because as soon as you start adding
shape or textboxes, things don't add up...

Try the above with a header and some text, as expected you will get 2
rectangles.
Now add a text box... now you have 6 rectangles!

If you run this code:

'_______________________________________
With ActiveWindow.Panes(1).Pages(1)
If .Rectangles.Count 1 Then
For i = 1 To .Rectangles.Count
MsgBox .Rectangles(i).RectangleType _
& vbCrLf & .Rectangles(i).Range.StoryType
Next
End If
End With
'_______________________________________

It will error out on rectangle 5 and 6 because those do not have ranges:

Rectangle 1 = 0 7 (Text rectangle PrimaryHeader Story)
Rectangle 2 = 0 1 (Text rectangle Main Story)
Rectangle 3 = 1 1 (Shape rectangle Main Story)
Rectangle 4 = 0 5 (Text rectangle Textframe Story)
Rectangle 5 = 7 n/a (System rectangle) The helpful help says "Not
applicable" as a description of the System type...
Rectangle 6 = 6 n/a (Selection rectangle) This is the actual anchor for the
shape.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site:
http://www.word.mvps.org


  #8  
Old February 3rd, 2006, 10:46 PM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Yes, that was about as far as I got, too. So how do you count the lines
within a table cell?



"Jean-Guy Marcil" NoSpam@LeaveMeAlone wrote in message
...
Jezebel was telling us:
Jezebel nous racontait que :

I've followed it far enough to establish that you can retrieve the
individual lines on the page; but quite how, I don'tknow.


Something like this:

'_______________________________________
Dim i As Long
Dim strTargetLine As String

With ActiveWindow.Panes(1).Pages(1)
If .Rectangles.Count 1 Then
For i = 1 To .Rectangles.Count
If .Rectangles(i).RectangleType = wdTextRectangle Then _
If .Rectangles(i).Range.StoryType = wdMainTextStory Then
Exit For
Next
End If
strTargetLine = .Rectangles(i).Lines(5).Range.Text
End With
'_______________________________________

You have to check for the rectangle type because as soon as you start
adding shape or textboxes, things don't add up...

Try the above with a header and some text, as expected you will get 2
rectangles.
Now add a text box... now you have 6 rectangles!

If you run this code:

'_______________________________________
With ActiveWindow.Panes(1).Pages(1)
If .Rectangles.Count 1 Then
For i = 1 To .Rectangles.Count
MsgBox .Rectangles(i).RectangleType _
& vbCrLf & .Rectangles(i).Range.StoryType
Next
End If
End With
'_______________________________________

It will error out on rectangle 5 and 6 because those do not have ranges:

Rectangle 1 = 0 7 (Text rectangle PrimaryHeader Story)
Rectangle 2 = 0 1 (Text rectangle Main Story)
Rectangle 3 = 1 1 (Shape rectangle Main Story)
Rectangle 4 = 0 5 (Text rectangle Textframe Story)
Rectangle 5 = 7 n/a (System rectangle) The helpful help says "Not
applicable" as a description of the System type...
Rectangle 6 = 6 n/a (Selection rectangle) This is the actual anchor for
the shape.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site:
http://www.word.mvps.org



  #9  
Old February 4th, 2006, 06:30 AM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

Jezebel was telling us:
Jezebel nous racontait que :

Yes, that was about as far as I got, too. So how do you count the
lines within a table cell?



I do not think you can do it with the Line property of the Rectangle
property. In fact, a table row will count a one line in the Count property
of the Lines property, regardless of the actual number of lines in any of
the cells in that row.

I think Helmut has an elegant solution, no?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site:
http://www.word.mvps.org


  #10  
Old February 6th, 2006, 02:32 PM posted to microsoft.public.word.tables,microsoft.public.word.vba.general
external usenet poster
 
Posts: n/a
Default How many lines used in a table cell?

I found it! It actually works, too - at least in Word 2000.

Sub Foo_countMyLines()

' To count lines in a table cell,
' select all text, BUT NOT the cell marker
' If the cell marker is selected and
' included in the range, the code
' returns "0" lines

Const wdStatisticLines = 1
Dim rng As Range
Set rng = Selection.Range
MsgBox "Lines: " & rng.ComputeStatistics(wdStatisticLines)

End Sub

Ed
"Jean-Guy Marcil" NoSpam@LeaveMeAlone wrote in message
...
Jezebel was telling us:
Jezebel nous racontait que :

Yes, that was about as far as I got, too. So how do you count the
lines within a table cell?



I do not think you can do it with the Line property of the Rectangle
property. In fact, a table row will count a one line in the Count property
of the Lines property, regardless of the actual number of lines in any of
the cells in that row.

I think Helmut has an elegant solution, no?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site:
http://www.word.mvps.org




 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add New Field to DB Karen Database Design 7 October 19th, 2005 08:03 PM
Help again from Ken Snell (Query) Randy Running & Setting Up Queries 22 August 29th, 2005 08:15 PM
copying cell names Al General Discussion 3 August 11th, 2005 03:01 PM
Access combo box-show name, not ID, in table? write on New Users 30 April 30th, 2005 09:11 PM
COMPARE THE TWO TABLES Stefanie General Discussion 0 June 4th, 2004 04:36 PM


All times are GMT +1. The time now is 07:10 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.