Question Locked

Macro for Deleting a page based on a text search

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Word 2003 Macro question:
I have 14 x 320 page word documents (School subject reports) created via mail merges. At irregular intervals in each document there will be a page which I wish to delete (the student does not do this subject). There is text present from the mail merge template, however there is no data inserted into this page. For example, on the pages I wish to delete the teacher name is blank.
I have created a macro and attached it to a toolbar so that I can delete the active page manually as I scroll through the document. However I would rather have the macro search through the document, and where it finds the string "Subject Teacher : " with no name next to it, then it can call the delete page macro I made. However I cannot figure out how to make it search for that string PLUS the missing teacher name (many different teacher names).
Can anybody help with this?

2 replies

by damien.griffiths

I was offered the following solution to my problem on another forum. It works very well. I am posting it here below for future refernce. Thanks to Jean-Guy for composing it.

--------------------------

Try something like the following. You will need to find a way to determine
the exact string you need to search for when there are no teacher names
(strSearch).

'_______________________________________
Dim strSearch As String
Dim rgeStart As Range

Set rgeStart = Selection.Range

strSearch = "Subject Teacher :" & Chr(13)

With ActiveDocument.Range.Find
.Text = strSearch
Do While .Execute
With .Parent
.Select
With Selection
.Bookmarks("\Page").Range.Delete
.Characters(1).Delete
End With
End With
Loop
End With

rgeStart.Select

Application.Browser.Target = wdBrowsePage
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

This discussion is closed to new replies.