Prev: Microsoft Responds to the Evolution of Online Communities
Next: Prevent word from accessing excel file on open
From: reubenhelms on 5 May 2010 00:08 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
From: Pesach Shelnitz on 5 May 2010 04:10 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 -- Hope this helps, Pesach Shelnitz My Web site: http://makeofficework.com "reubenhelms" wrote: > 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
From: Doug Robbins - Word MVP on 5 May 2010 04:17 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 "reubenhelms" <reubenhelms(a)discussions.microsoft.com> wrote in message news:C436A79A-EAEE-4175-8112-30339A988FBC(a)microsoft.com... > 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
From: Fumei2 via OfficeKB.com on 5 May 2010 15:51 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 Doug Robbins - Word MVP wrote: >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
From: Doug Robbins - Word MVP on 5 May 2010 18:21
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" <u53619(a)uwe> wrote in message news:a7906963c6752(a)uwe... > 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 > > Doug Robbins - Word MVP wrote: >>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 > |