Consulting

Results 1 to 2 of 2

Thread: Help with deleting text between headings

  1. #1
    VBAX Newbie
    Joined
    Aug 2008
    Posts
    2
    Location

    Help with deleting text between headings

    Hello,

    I've been trying to come up with a macro that will identify a heading based on a particular style, and then delete everything under that heading (including any sub-headings and their contents). I can get to where I find the heading that I want, and I can delete it, but not the following contents under it.

    Thank you for any help!!
    SW

  2. #2
    VBAX Newbie
    Joined
    Aug 2008
    Posts
    2
    Location
    Hello,

    It's currently looking based on a style. So if it finds a style called "Int Heading 2", it should delete everything until it finds another "Int Heading 2" or a "Int Heading 1" (basically, the current level heading or greater).

    To put it another way:

    1 Chapter on XYZ
    1.1 Sub-chapter of XYZ
    1.1.1 Another sub-chapter
    1.2 Sub-chapter of XYZ
    2 Chapter on ABC

    Let's say 1.1 had a specific chapter heading style "Int Heading 2", while the other headings just have the regular style "Heading #". I would then want to delete all of 1.1, including 1.1.1, but not 1.2. Unless of course that 1.2 also had the "Int Heading #" style.

    Here's my starting point (leveraged from old code), and I can't figure out how to expand it to search for the next heading of a <= level and delete everything in between:

    With Selection.Find
    .ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Int Heading 2")
    Text = ""
    With .Replacement
    .ClearFormatting
    .Text = ""
    End With
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With

    I'm a newbie with VBA, so help is definitely appreciated!!
    SW

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •