Link to home
Start Free TrialLog in
Avatar of chris_desborough
chris_desboroughFlag for Australia

asked on

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

The logic of what you need isn't quite clear. Perhaps you should say what goes wrong with the code that you have posted?
Avatar of chris_desborough

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of chris_desborough
chris_desborough
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial