Select text to copy and paste

J

j kaurloto

To the group:

I have numerous long documents that are compilations of email messages.
The messages are delimited with the lines:
=====Begin Message=====
and
=====Begin Message=====
throughout the documnets.

I would like to write a macro that loops through the document and selects
the text from Begin Message to End Message and copies and paste it into a
new document and then does the next one etc., until the end of the document.

I have the basics, like copy, paste, create a new document, etc. Fairly
straightforward stuff.

I am at a total loss on how to loop through the document and select the text
from each group of
=====Begin Message===== to =====Begin Message=====

Any suggestions or guidance greatly appreciated.

Thank you, most kindly.

John
 
G

Greg Maxey

John,

Provided the demiting is precisely as you indicate something like this
should do:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "=====Begin Message=====*=====Begin Message====="
.MatchWildcards = True
While .Execute
oRng.MoveStart wdCharacter, 23
oRng.MoveEnd wdCharacter, -23
MsgBox oRng.Text 'Put this text in you new document
oRng.Collapse wdCollapseEnd
Wend
End With
With oRng.Find
.Text = "=====Begin Message=====" & Chr(13) & "*" & Chr(13)
.Execute
oRng.MoveStart wdCharacter, 23
MsgBox oRng.Text 'This is the last message text
End With
End Sub
 
J

j kaurloto

Thank you!

I do have a question if I may. I do a lot of VBA in Access and I am not
very familiar with the VBA in Word.
I have added the following to copy the text, add a new document and paste
the text

While .Execute
oRng.MoveStart wdCharacter, 23
oRng.MoveEnd wdCharacter, -23

oRng.Copy
Documents.Add
Selection.HomeKey Unit:=wdStory
oRng.Paste

oRng.Collapse wdCollapseEnd
Wend

It adds the new documnet, and it copies (I can paste from the edit menu) but
it doesn't paste to the new document. What am I doing wrong?
My apologies for being so dense. Apparently I should just stick to
databases...
 
J

j kaurloto

Never mind - I fixed it...
My sincere thanks and gratitude.

While .Execute
oRng.MoveStart wdCharacter, 23
oRng.MoveEnd wdCharacter, -23
oRng.Copy

Documents.Add.Content.Paste

oRng.Collapse wdCollapseEnd
Wend
 

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