0
votes

I have sub sections in a word document which I want to delete based on user inputs in a custom user interface.

For e.g : I want to delete sub section 3.1.1 under the section 3

I used the following code, but it deletes the entire section but I want to delete only a specific sub section:

ActiveDocument.Sections(x).Range.Delete

Here I am not able to give x = 3.1.1, it only accepts just the integer value like 3 and that deletes the entire section.

1

1 Answers

0
votes

Word doesn't have nested sections, so you probably need to cycle through the sections until you find one that matches your needs. So, let's say you have a Word document that looks like this:

Title___[continuous section break]
Stuff
Section 1___[continuous section break]
Stuff
Section 1.1___[continuous section break]
Stuff

You could loop through the sections and check the first paragraph of each:

For each objSect in ActiveDocument.sections
    if trim(replace(objSect.Range.Paragraphs.First.range.Text, chr(13), "")) like "* 1.1" then objSect.range.delete
Next objSect

Of course, that means if you want to delete section 1 you'll need to delete it along with any sub-sections, one at a time.

If what you really want is something like what the navigation pane gives you, I don't know if that's supported in VBA. There don't appear to be any methods that would mimic the Navigation Pane Delete option.