Select Body String - great code needs refinement

J

Jen

Hi,

I tried this code and added Body.Delete and Body.Copy

BUT on text below it selects from BEGINNING OF DOC to last character - it
does not select period at end of sentence. It is supposed to select from
below DEAR.
How do I refine the code to select text between DEAR and also select the
period in the last sentence before YOURS.

I want to COPY the selection
And to DELETE the selection.

My date is a FIELD and starts at top of page on a SAVED document.

28 October 2006


Dear Mr Jones



Heading

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

Yours faithfully

XXXXXX



***CODE STARTS
Public Sub BodyDelete()
Dim Body As Range
Set Body = ActiveDocument.Range
Body.Start = Body.Start + InStr(Body, "Dear")
Body.Start = Body.Start + Len(Body.Paragraphs(1).Range) - 1
Body.End = Body.Start + InStr(Body, "Yours") - 1
Body.Select
Body.Copy
Body.Delete

End Sub

Thanks for any ideas.
 
D

Doug Robbins - Word MVP

Assuming that what you have in the document is not all one paragraph, but is
separated into seperate paragraphs as indicated in the following by the ¶

Dear Mr Jones¶

Heading¶

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.¶

Yours faithfully¶

The code selects from the H of Heading to the period after dog

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

Jen

No it doesn't Doug when the date is a CREATE DATE FIELD it selects from the
top of the document and stops on last character before fullstop on last
sentence.

Does a field upset a Range selection?
 
D

Doug Robbins - Word MVP

Use:

Dim Body As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Dear"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Set Body = Selection.Range
Body.End = ActiveDocument.Range.End
'Body.Start = Body.Start + InStr(Body, "Dear")
Body.Start = Body.Start + Len(Body.Paragraphs(1).Range) ' - 1
Body.End = Body.Start + InStr(Body, "Yours") - 1
Body.Select


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

Jen

Many thanks Doug - that did the trick.

Doug Robbins - Word MVP said:
Use:

Dim Body As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Dear"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Set Body = Selection.Range
Body.End = ActiveDocument.Range.End
'Body.Start = Body.Start + InStr(Body, "Dear")
Body.Start = Body.Start + Len(Body.Paragraphs(1).Range) ' - 1
Body.End = Body.Start + InStr(Body, "Yours") - 1
Body.Select


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

fred

Doug,
I do have one issue if there is a table with "YOUR" which I now use "OUR
REF" (so YOUR as not workable). If table contains "OUR REF" I get an error
for RANGE.
Is there a way around that?

Many tks
 

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