Select all of text in cell

K

Karen

ARRGGH!

I have a cell with text that wraps to a second line (the second line is also
indented). It look likes

Description1: This is the fourth picture in a series
of eighteen pictures which were taken

I am trying to select all of the text that follows 'Description:^t', so my
code looks like

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Description:^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Extend
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Caption" & i
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.EscapeKey

The only problem is that the

Selection.Extend
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

part just goes to the end of the first line, not to the end of the text in
the cell. There is no paragraph mark at the end of the first line, it just
wraps to the second indented line.

How can I select all of the text following 'Description:^t' ?
 
J

Jean-Guy Marcil

Hi KAren,

Try this:

'_______________________________________
Dim BookmarkRng As Range

Selection.HomeKey wdStory

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Description1:^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1

Set BookmarkRng = Selection.Range
BookmarkRng.SetRange BookmarkRng.Start, _
Selection.Paragraphs(1).Range.End - 1

'"-1" to unselect the cell marker
'Note that this will not work if you have many
'paragraphs in the cell and want all of them.
'If you have many paragraphs, use:

'BookmarkRng.SetRange BookmarkRng.Start, _
' Selection.Cells(1).Range.End - 1

With ActiveDocument.Bookmarks
.Add Range:=BookmarkRng, Name:="Caption" & i
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
End If
'_______________________________________

Whenever you can, avoid the Selection object... It will make your life
easier in the long run!

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

Karen

Jean-Guy,

Thanks so much, this works super.

--
Karen

Jean-Guy Marcil said:
Hi KAren,

Try this:

'_______________________________________
Dim BookmarkRng As Range

Selection.HomeKey wdStory

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Description1:^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1

Set BookmarkRng = Selection.Range
BookmarkRng.SetRange BookmarkRng.Start, _
Selection.Paragraphs(1).Range.End - 1

'"-1" to unselect the cell marker
'Note that this will not work if you have many
'paragraphs in the cell and want all of them.
'If you have many paragraphs, use:

'BookmarkRng.SetRange BookmarkRng.Start, _
' Selection.Cells(1).Range.End - 1

With ActiveDocument.Bookmarks
.Add Range:=BookmarkRng, Name:="Caption" & i
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
End If
'_______________________________________

Whenever you can, avoid the Selection object... It will make your life
easier in the long run!

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Glad I could help :)

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


Karen said:
Jean-Guy,

Thanks so much, this works super.

--
Karen

Jean-Guy Marcil said:
Hi KAren,

Try this:

'_______________________________________
Dim BookmarkRng As Range

Selection.HomeKey wdStory

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Description1:^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1

Set BookmarkRng = Selection.Range
BookmarkRng.SetRange BookmarkRng.Start, _
Selection.Paragraphs(1).Range.End - 1

'"-1" to unselect the cell marker
'Note that this will not work if you have many
'paragraphs in the cell and want all of them.
'If you have many paragraphs, use:

'BookmarkRng.SetRange BookmarkRng.Start, _
' Selection.Cells(1).Range.End - 1

With ActiveDocument.Bookmarks
.Add Range:=BookmarkRng, Name:="Caption" & i
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
End If
'_______________________________________

Whenever you can, avoid the Selection object... It will make your life
easier in the long run!

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


is
also
so
text
 

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