Accessing Heading in a Word Document RRS feed

  • Question

  • Hi All,

    I have a lenghty word document, content of which is organized under headings and sub headings. I want to edit the document so as to keep the entire heading (and the content) depending on some logic and remove the ones which i dont want to keep.

    The powershell logic is able to determine the determine the heading i want to keep.

    However, I am not able to delete the content in an efficient way. What i am currently doing is -- 

    1. Iterating through the paragraphs of the word doc.

    2. If the Style is ChapterHeading and the Text matches my list, I set a flag. I keep the stuff when the flag is set. I unset the flat when  Style is ChapterHeading and the Text matches doesnt not match my list. I delete everything when the flag is unset.

    The problem is -- 

    My document is lengthy and processing takes long time to delete it paragraph wise.

    Alternatively, i want to achieve deletion of the entire heading stuff like the way we do it from NavigationPane of word. I cant attach a image here. But if you select a heading from the navigation pane, you can right click and delete all content under it in a single action

    The below image shows what i want to do in my powershell code.. Any pointers on how I can do it?


    Tuesday, May 30, 2017 11:59 AM

All replies

  • Without a clear explanation and a sample of the code it is not possible to understand what you are trying to ask. 

    PowerShell does not work as you are suggesting.  It would use the object model of word and search for paragraphs.  If the paragraphs are part of an outline or define sections then it can very efficiently do what you ask.  If it is a poorly designed document then you will have to create a more complex bit of code.

    First you need to post the code you are trying to use.


    Tuesday, May 30, 2017 12:32 PM