Get a Range for 2nd line in cell

P

Phil Vern

I have a cell in a table from which I want to get a Range that includes only
the second line of text in the cell. At the moment I am looping through the
Characters collection, using the first and second return characters to
identify the second line. The problem is when I go to get the range from
them, I get a Type mismatch error. How do I get the Range from these two
characters? Thanks for your help.

Sub Test1()

Dim c As Word.Cell, r As Word.Range
Dim r2 As Word.Range
charRange As Word.Range
Dim lngStartOfRange As Long, lngEndOfRange As Long
Dim rngStartOfRange As Word.Range, rngEndOfRange As Word.Range

Set c = ActiveDocument.Tables.Item(1).Cell(7, 4)
Set r = c.Range

' Find starting and ending character positions.
Dim i As Integer, str As String
i = 0
For Each charRange In r.Characters
i = i + 1
If Asc(charRange) = 13 Then
If rngStartOfRange Is Nothing Then
Set rngStartOfRange = r.Characters(i + 1)
Else
Set rngEndOfRange = r.Characters(i - 1)
End If
End If
If Not rngEndOfRange Is Nothing Then Exit For
Next
Set r2 = ActiveDocument.Range(rngStartOfRange, rngEndOfRange)
MsgBox "Line 2 text: " & r2.Text

End Sub
 
H

Helmut Weber

Hi Phil,

you are dealing with paragraphs, not lines.
Untested, pseudocode!
Selection.range.cells(1).select
Selection.range.paragraphs(2).select

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Phil,

If you are using return characters in each line then perhaps something
like this will do:

Sub Test()
Dim oRng As Range
Dim myString As String

Set oRng = ActiveDocument.Tables(1).Cell(7, 4).Range
myString = oRng.Paragraphs(2).Range.Text
MsgBox Left(myString, Len(myString) - 2)


End Sub
 
P

Phil

Thanks Helmut. I followed your suggestion to use the paragraph collection to
get a range for the second line in a cell and it works of course.
 
P

Phil

Thanks Greg. This is a good solution if I only wanted to get the text of the
second line. I actually needed to get the range for the second line, so I
could then get the Hyperlinks collection in that range.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top