First line in a cell

S

StevenM

Sub FindCellsInTable()
Dim oTable As Table
Dim nTables As Long
Dim nRows As Long
Dim nColumns As Long
Dim nIndex As Long

nTables = ActiveDocument.Tables.Count
If nTables > 0 Then
For nIndex = 1 To nTables
Set oTable = ActiveDocument.Tables(nIndex)
nRows = oTable.Rows.Count
nColumns = oTable.Columns.Count
sStr = oTable.Cell(1, 1).Range.Text
sStr = Left(sStr, Len(sStr) - 2)
MsgBox "Table #" & nIndex & " has " _
& nRows & " row(s) and " _
& nColumns & " column(s)." & vbCr _
& "Cell(1,1) = " & sStr
Next nIndex
End If
End Sub

Steven Craig Miller
 
D

DMc2004

If a cell contains the following:

abcdefg
123456
!"£$%^

I would like returned abcdefg, but not to have

abcdefg
123456
!"£$%^

returned.

Regards
 
S

StevenM

If a cell contains the following:
abcdefg
123456
!"£$%^

I would like returned abcdefg, but not to have

123456
!"£$%^

Is each line a different paragraph? (A "paragraph" includes any line -- even
an empty line -- which ends with a paragraph mark.)

Sub FindFirstLineOfCell()
Dim nIndex As Long
Dim nTables As Long

nTables = ActiveDocument.Tables.Count
If nTables > 0 Then
For nIndex = 1 To nTables
sStr = ActiveDocument.Tables(nIndex).Cell(1, 1).Range.Text
sStr = Left(sStr, InStr(sStr, vbCr) - 1)
MsgBox "First line of Cell(1,1) = " & sStr
Next nIndex
End If
End Sub

While each cell ends with a Carriage Return and Line Feed, each paragraph
before the last line of a cell merely ends with a paragraph mark.

The line: sStr = Left(sStr, InStr(sStr, vbCr) - 1) returns the left part of
the string before the first paragraph mark.

If that doesn't work for you, we can look for the first word in a cell, or
the first word without a digit in the cell, or something distinctive.

Steven Craig Miller
 
H

Helmut Weber

Hi D,

there is more to it than meets the eyes:

Sub Test400c()

Dim l1 As Long
Dim l2 As Long
Dim sTmp As String
Dim rTmp1 As Range
Dim rTmp2 As Range
Set rTmp1 = ActiveDocument.Tables(1).Cell(2, 2).Range ' that cell
If rTmp1.Text = Chr(13) & Chr(7) Then
Exit Sub
End If
rTmp1.End = rTmp1.End - 1
If rTmp1.ComputeStatistics(wdStatisticLines) = 1 Then
sTmp = rTmp1.Text
MsgBox "[" & sTmp & "]"' result
Exit Sub
End If
Set rTmp2 = rTmp1.Duplicate
rTmp1.Collapse
rTmp2.Collapse
' rTmp1.Select ' for testing only
l1 = rTmp1.Information(wdFirstCharacterLineNumber)
l2 = l1
While l1 = l2
rTmp1.End = rTmp1.End + 1
rTmp1.Select
rTmp2.start = rTmp1.End
rTmp2.End = rTmp1.End
l2 = rTmp2.Information(wdFirstCharacterLineNumber)
Wend
sTmp = rTmp1.Text
If Right(sTmp, 2) = Chr(13) + Chr(7) Then
sTmp = Left(sTmp, Len(sTmp) - 2)
End If
If Right(sTmp, 1) = Chr(13) Then
sTmp = Left(sTmp, Len(sTmp) - 1)
End If
If Right(sTmp, 1) = Chr(11) Then
sTmp = Left(sTmp, Len(sTmp) - 1)
End If
If Right(sTmp, 1) = Chr(32) Then
sTmp = Left(sTmp, Len(sTmp) - 1)
End If
MsgBox "[" & sTmp & "]" ' result
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jean-Guy Marcil

DMc2004 said:
How can I get the first line of text from a cell of a table.

Regards

Try this:

Option Explicit

Sub FirstCellLine()

Dim rngCell As Range
Dim strFirstLine As String
Dim rngCurr As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place the cursor in a cell.", vbCritical, _
"Wrong Selection"
Exit Sub
End If

Set rngCurr = Selection.Range

Set rngCell = Selection.Cells(1).Range.Words(1)
rngCell.Select
strFirstLine = rngCell.Bookmarks("\Line").Range.Text

rngCurr.Select

MsgBox strFirstLine

End Sub
 
F

fumei via OfficeKB.com

Assumptions:

1. Table is Table#1 - ActiveDocument.Tables(1)
2. Text of:

Abcdefg
123456
!"£$%^

is in Cell(1,1) - the first cell

3. Abcdefg is terminated by a paragraph mark

THEN:

MsgBox ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs(1)

will display: Abcdefg
 

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