using vba to delete part of a document

B

Brian Beck

I've got a group of letters that are generated through a mail merge with an
excel file. These letters are to various entities and contain a listing of
specific incidents that they were involved in. Since I wanted to just have
one template and since the number of incidents per letter will vary, I
simply created a template that had X number of incident placeholders and
when the merge was performed, the placeholders were only populated up to the
number of incidents that particular entity had. For example, right now the
template has 39 placeholders because that happens to be the largest number
of incidents in any one letter. If I merge the data for an entity who has 6
incidents, then the resulting letter will contain those 6 incidents,
followed by 33 incident placeholders that contain no data.

Currently, I have to go back in to each letter and delete the "extra"
placeholders that are left over from the mail merge. (I know, I know..there
is probably a much easier way for me to do all of this from the outset, but
this is what I'm currently stuck with.) I've managed to place an identifier
in the excel file so that when the data is merged into letters, this
identifier is always found after the last incident in the letter...thus I
know that everything after the identifier until the language at the bottom
of the letter that is the same for all the letters can be deleted...that is,
all the rest of the incidents are simply blank placeholders.

What I would like to be able to do is create a macro that will go through a
letter and remove those placeholders for me. I'm thinking along these
lines, but I don't know how to make Word do this:

1. Find the identifier in the letter and move the cursor there.
2. Mark where the cursor is.
3. Find the beginning text at the bottom of the letter that marks the ending
of the incident placeholders and place the cursor at the beginning of the
paragraph.
4. Mark where the cursor is.
5. Now delete everything that is between the first and second marks.

Any ideas on how to accomplish this? Or possibly even a better way to go
about it?

-Brian
 
G

Greg Maxey

Brian,

Lets say your identifier is the sting %%%%%%%

Set a bookmark named SetPoint2 at the beginning of the test that you
want to keep at the end of the document. Then run:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "%%%%%%%"
.Execute
If .Found Then
ActiveDocument.Bookmarks.Add "SetPoint1", oRng
End If
End With
oRng.Start = ActiveDocument.Bookmarks("SetPoint1").Range.Start
oRng.End = ActiveDocument.Bookmarks("SetPoint2").Range.End
oRng.Delete
End Sub
 

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