Selectively delete Word pages with VBA depending on content

my word document has page breaks so pages are easily identified.  I need to either delete pages with nominated text OR only include pages with nominated text.   After I have page with the correct title (not shown in example below), I have to either keep or delete it, depending on the action parameter.  This code needs to be in a template as new word documents have to be processed each day (vbs job driven by an excel worksheet).
Any help will be greatly appreciated!


Sub SearchPage(pagenum)

' action is I to include or E to exclude

Dim r As Range
Dim found As Boolean
Dim newpage As Integer
found = True
   
    Selection.Find.ClearFormatting
    MsgBox Selection.Text
    With Selection.Find
        .Text = mtext
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
   
    If Selection.Find.Execute Then
    'MsgBox "found"
        newpage = Selection.Information(wdActiveEndPageNumber)
        MsgBox pagenum & " " & newpage
        If newpage = pagenum Then
            If maction = "E" Then
                Selection.Bookmarks("\Page").Range.Delete
                rcount = rcount + 1
               
            End If
         End If
     Else
        If maction = "I" Then
           Selection.Bookmarks("\Page").Range.Delete
           rcount = rcount + 1
        End If
     End If
   
 
End Sub
LVL 1
chris_desboroughAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
The logic of what you need isn't quite clear. Perhaps you should say what goes wrong with the code that you have posted?
0
chris_desboroughAuthor Commented:
My code just never ends.  I  have been having difficulty debugging with the code is in a template so I guess I need to suppress the frustration and put the code in my document for debugging.  I will do this and then ask again.
0
chris_desboroughAuthor Commented:
The looping was caused by the fact that the search started again at page 1 after reaching the end of the document.  I thought it would end.  Once I catered for this, it is working fine.  Sorry to bother you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.