Selecting some text in a Word table Cell

R

reubenhelms

I'm trying to write some Word VBA to select some text in a table cell. The
selection is to start from the first instance of a paragraph marker in the
cell, and extend to the end of cell marker.

The idea is that I might have a cell with CellLine1^pCellLine2^pCellLine3
and want to cut all text from the first paragraph marker to the end of the
cell, leaving only the first line.

So far, I have:

aCell.Select
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
With Selection
.StartIsActive = False
.EndOf Unit:=wdCell, Extend:=wdExtend
.MoveEnd Unit:=wdCharacter, Count:= -1

.Cut
End With
End If

The finding of the paragraph marker is fine, but when the selection moves to
the end of the cell, the whole cell is selected, and I lose the start marker
at the beginning of the paragraph mark.

I've trying using .MoveStartUntil("^p") after the move end to move the
selection to the start of that first paragraph marker, but that didn't seem
to work.

Any pointers?

Regards
Reuben Helms
 
P

Pesach Shelnitz

Hi,

Here is a modified version of your macro that keeps only the first line in
the table cell where the cursor is located and cuts the rest of the text in
the cell.

Sub KeepFirstLineInTableCell()
Dim MyRange As Range

Set MyRange = Selection.Cells(1).Range
MyRange.Select
Selection.Collapse Direction:=wdCollapseStart
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindStop
If .Execute = True Then
Selection.End = MyRange.End - 1
Selection.Cut
End If
End With

Set MyRange = Nothing
End Sub
 
D

Doug Robbins - Word MVP

Use:

Dim rng As Range
Set rng = Selection.Cells(1).Range.Paragraphs(2).Range
rng.End = rng.Cells(1).Range.End - 1
rng.Select

If your range includes the end of cell marker, when you use select, the
whole cell will be selected. Hence the rng.Cells(1).Range.End - 1 to
exclude the end of cell marker.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
F

Fumei2 via OfficeKB.com

I fail to see why you need to deal with moving ranges at all. Just make the
range.text of the cell equal the range.text of paragraph(1). Done.

If there are multiple lines then there are paragraph marks.

To KEEP the paragraph mark (paragraph mark PLUS end-of cell)

Sub KeepPMark()
Selection.Cells(1).Range.Text = _
Selection.Cells(1).Range.Paragraphs(1).Range.Text
End Sub

To REMOVE the paragraph mark (text then end-of-cell)

Sub RemoveP_Mark()
Selection.Cells(1).Range.Text = _
Left(Selection.Cells(1).Range.Paragraphs(1).Range.Text, _
Len(Selection.Cells(1).Range.Paragraphs(1).Range.Text) - 1)
End Sub

Gerry
Use:

Dim rng As Range
Set rng = Selection.Cells(1).Range.Paragraphs(2).Range
rng.End = rng.Cells(1).Range.End - 1
rng.Select

If your range includes the end of cell marker, when you use select, the
whole cell will be selected. Hence the rng.Cells(1).Range.End - 1 to
exclude the end of cell marker.
I'm trying to write some Word VBA to select some text in a table cell.
The
[quoted text clipped - 39 lines]
Regards
Reuben Helms
 
D

Doug Robbins - Word MVP

To cut or delete everything EXCEPT the first paragraph, you must set a range
to everything EXCEPT that paragraph, or set a range to the first paragraph,
copy it to the clipboard, then delete ALL of the contents of the cell and
then paste from the clipboard back to the cell.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Fumei2 via OfficeKB.com said:
I fail to see why you need to deal with moving ranges at all. Just make
the
range.text of the cell equal the range.text of paragraph(1). Done.

If there are multiple lines then there are paragraph marks.

To KEEP the paragraph mark (paragraph mark PLUS end-of cell)

Sub KeepPMark()
Selection.Cells(1).Range.Text = _
Selection.Cells(1).Range.Paragraphs(1).Range.Text
End Sub

To REMOVE the paragraph mark (text then end-of-cell)

Sub RemoveP_Mark()
Selection.Cells(1).Range.Text = _
Left(Selection.Cells(1).Range.Paragraphs(1).Range.Text, _
Len(Selection.Cells(1).Range.Paragraphs(1).Range.Text) - 1)
End Sub

Gerry
Use:

Dim rng As Range
Set rng = Selection.Cells(1).Range.Paragraphs(2).Range
rng.End = rng.Cells(1).Range.End - 1
rng.Select

If your range includes the end of cell marker, when you use select, the
whole cell will be selected. Hence the rng.Cells(1).Range.End - 1 to
exclude the end of cell marker.
I'm trying to write some Word VBA to select some text in a table cell.
The
[quoted text clipped - 39 lines]
Regards
Reuben Helms

--
Gerry

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201005/1
 

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