chris_desborough
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.ClearFormat ting
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(wdAc tiveEndPag eNumber)
MsgBox pagenum & " " & newpage
If newpage = pagenum Then
If maction = "E" Then
Selection.Bookmarks("\Page ").Range.D elete
rcount = rcount + 1
End If
End If
Else
If maction = "I" Then
Selection.Bookmarks("\Page ").Range.D elete
rcount = rcount + 1
End If
End If
End Sub
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.ClearFormat
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(wdAc
MsgBox pagenum & " " & newpage
If newpage = pagenum Then
If maction = "E" Then
Selection.Bookmarks("\Page
rcount = rcount + 1
End If
End If
Else
If maction = "I" Then
Selection.Bookmarks("\Page
rcount = rcount + 1
End If
End If
End Sub
The logic of what you need isn't quite clear. Perhaps you should say what goes wrong with the code that you have posted?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.