View Full Version : Solved: delete certain pages only in section 2 of a document
jiggly
05-15-2013, 06:19 AM
I am trying to figure out a way to delete pages only in section 2 if they match a certain criteria
here is my current code
Dim mm As Range
Set mm = ActiveDocument.Sections(2).Range
With mm.Find
.Style = "Caption,+++Caption,+Caption"
Do While .Execute(FindText:="viewgraph", Forward:=True, MatchWholeWord:=True) = True
With .Parent
            .Select
            Selection.GoTo What:=wdGoToBookmark, Name:="\page"
    Selection.Delete Unit:=wdCharacter, Count:=1
 End With
Loop
End With
the code works fine except it wont stop at the end of section 2, and continues to run rampant through the rest of the document
any ideas?
TIA
macropod
05-19-2013, 11:57 PM
Try something based on:
Sub Demo()
Application.ScreenUpdating = False
Dim RngFnd As Range, RngDel As Range
With ActiveDocument.Range
  Set RngFnd = .Sections(2).Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "viewgraph"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Style = "Caption,+++Caption,+Caption"
    .MatchWildcards = False
    .MatchWholeWord = True
    .Execute
  End With
  Do While .Find.Found
    If .InRange(RngFnd) Then
      Set RngDel = .GoTo(What:=wdGoToPage, Name:=.Information(wdActiveEndAdjustedPageNumber))
      Set RngDel = RngDel.GoTo(What:=wdGoToBookmark, Name:="\page")
      RngDel.Text = vbNullString
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
jiggly
05-20-2013, 05:05 AM
Try something based on:
Sub Demo()
Application.ScreenUpdating = False
Dim RngFnd As Range, RngDel As Range
With ActiveDocument.Range
  Set RngFnd = .Sections(2).Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "viewgraph"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Style = "Caption,+++Caption,+Caption"
    .MatchWildcards = False
    .MatchWholeWord = True
    .Execute
  End With
  Do While .Find.Found
    If .InRange(RngFnd) Then
      Set RngDel = .GoTo(What:=wdGoToPage, Name:=.Information(wdActiveEndAdjustedPageNumber))
      Set RngDel = RngDel.GoTo(What:=wdGoToBookmark, Name:="\page")
      RngDel.Text = vbNullString
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
Paul, thanks, it didn't act as expected initially, and went outside the specified range, however I modified it slightly and it seems to be performing well... here is the adjusted code:
 Dim RngFnd As Range, RngDel As Range
    With ActiveDocument.Range
        Set RngFnd = .Sections(2).Range
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "Viewgraph"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .Style = "Caption,+++Caption,+Caption"
            .MatchWildcards = False
            .MatchWholeWord = True
            .Execute
        End With
        Do While .Find.Found
            If .InRange(RngFnd) Then
                Set RngDel = .GoTo(What:=wdGoToBookmark, Name:="\page")
                RngDel.Text = vbNullString
            End If
            .Collapse wdCollapseEnd
            .Find.Execute
        Loop
    End With
I'll let you know if i run into any issues during further testing.
thanks again for your input
i did put the screenupdating toggle back in btw, just left it out for testing
jiggly
05-21-2013, 04:26 AM
After a day of brutal testing, it seems to be pretty bulletproof. Thanks again, I just couldn't get it quite right on my own. Much appreciated
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.