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
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