Home > Blockchain >  How to delete content in a specific page range under certain heading level/style?
How to delete content in a specific page range under certain heading level/style?

Time:07-13

Suppose I have three levels/styles of headings in a document,

enter image description here

I would like to know how to delete all the normal text under each instance of the third heading level/style Heading 3 within a specific page range while keeping all the headings as they are.

Furthermore, I need to do the same to delete each table found for the same conditions after making sure the table has 11 rows.

CodePudding user response:

Try:

Sub DelHd3Content()
Application.ScreenUpdating = False
Dim RngFnd As Range, Rng As Range, t As Long, i As Long
Const StrHd1 As String = "General Electrical Engineering"
Const StrHd2 As String = "Systems and Artificial Intelligence"
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .Style = wdStyleHeading3
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Text = StrHd1
    .Execute
    If .Found = True Then
      Set RngFnd = .Parent.Paragraphs(1).Range
    Else
      MsgBox "First heading not found!", vbCritical: Exit Sub
    End If
    .Parent.Collapse wdCollapseEnd
    .Text = StrHd2
    .Execute
    If .Found = True Then
      Set Rng = .Parent.Paragraphs(1).Range
    Else
      MsgBox "Last heading not found!", vbCritical: Exit Sub
    End If
  End With
  RngFnd.End = Rng.Start
End With
With RngFnd.Duplicate
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Style = wdStyleHeading3
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
  End With
  Do While .Find.Execute
    If .InRange(RngFnd) = False Then Exit Do
    Set Rng = .Paragraphs(1).Range.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
    With Rng
      .Start = .Paragraphs(1).Range.End
      .Text = vbNullString: i = i   1
      For t = .Tables.Count To 1 Step -1
        With .Tables(t)
          If .Rows.Count = 11 Then .Delete: i = i   1
        End With
      Next
    End With
    .Collapse wdCollapseEnd
  Loop
End With
Set Rng = Nothing: Set RngFnd = Nothing
Application.ScreenUpdating = True
MsgBox i & " ranges updated.", vbOKOnly
End Sub

If the content under your 'Systems and Artificial Intelligence' heading is also meant to be processed, change:

  RngFnd.End = Rng.Start

to:

  RngFnd.End = Rng.End

CodePudding user response:

For example:

Sub DelHd3Content()
Application.ScreenUpdating = False
Dim RngFnd As Range, Rng As Range, t As Long
With ActiveDocument
  Set RngFnd = .Range.GoTo(What:=wdGoToPage, Name:=10)
  Set Rng = .Range.GoTo(What:=wdGoToPage, Name:=20)
  RngFnd.End = Rng.End
End With
With RngFnd.Duplicate
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Style = wdStyleHeading3
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
  End With
  Do While .Find.Execute
    If .InRange(RngFnd) = False Then Exit Sub
    Set Rng = .Paragraphs(1).Range.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
    With Rng
      .Start = .Paragraphs(1).Range.End
      '.Text = vbNullString
      For t = .Tables.Count To 1 Step -1
        With .Tables(t)
          If .Rows.Count = 11 Then .Delete
        End With
      Next
    End With
    .Collapse wdCollapseEnd
  Loop
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub

As coded, the macro deletes the tables on pages 10 through 20. To delete everything on those pages, uncomment '.Text = vbNullString' and (optionally) delete/comment-out the 'For t = .Tables.Count To 1 Step -1 ... Next' loop. You could leave that loop in, it would be redundant.

  • Related