remove all content controls from docx

User avatar
stuck
Panoramic Lounger
Posts: 8128
Joined: 25 Jan 2010, 09:09
Location: retirement

remove all content controls from docx

Post by stuck »

I'm missing something here, can't see what though. The code below removes all the content controls in my doc EXCEPT one that's in the Section 2 Footer, why does it miss that one control?

Code: Select all

    For Each rngStory In ActiveDocument.StoryRanges
        Do While rngStory.ContentControls.Count > 0
            For Each objCC In rngStory.ContentControls
                objCC.Delete
            Next
        Loop
    Next rngStory
:hairout:

Ken

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: remove all content controls from docx

Post by Rudi »

I'm no Word fundy, but I think StoryRanges refers to the document body.

Try this, or add to your code...

Code: Select all

    For Each rngStory In ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.ContentControls
        Do While rngStory.ContentControls.Count > 0
            For Each objCC In rngStory.ContentControls
                objCC.Delete
            Next
        Loop
    Next
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
stuck
Panoramic Lounger
Posts: 8128
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: remove all content controls from docx

Post by stuck »

I've found the answer. My Googling finally led me to this post on MSDN, which in turn led me to Step 2 on this Word MVP page and from that I altered my code to:

Code: Select all

    Dim lngJunk As Long

    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
    For Each rngStory In ActiveDocument.StoryRanges
    
        Do
            For Each objCC In rngStory.ContentControls
                objCC.Delete
            Next
        Set rngStory = rngStory.NextStoryRange
        Loop Until rngStory Is Nothing
    
    Next
The root of the issue appears to be that Word can't cope with unlinked headers and footers in this context (and my Section 2 footer is not linked to previous). The workaround therefore is to set the variable lngJunk to the relevant StoryType and now that it's a story type it gets included in the For Each loop and thus the unlinked Footer is processed.

I hope someone can confirm / correct my understanding but meanwhile it does the job :grin:

Ken

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: remove all content controls from docx

Post by Rudi »

Glad you came right, and TX for the code update.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Guessed
2StarLounger
Posts: 102
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: remove all content controls from docx

Post by Guessed »

I think the problem you are encountering is more to do with the order you are deleting CCs in a loop. Your code is effectively saying - here is a stack of Content Controls, moving up the stack and delete each one you come to. The problem is that by deleting the first one causes all the others to advance one position along the stack. Therefore the second one you are deleting used to be the third one and the second one is now the first one. Effectively, this makes the code delete every second CC.

There are two ways to avoid this. The first way moves backwards from the end of the line.

Code: Select all

For i = rng.ContentControls.Count to 1 step -1
  rng.ContentControls(i).Delete
Next i
The second way keeps deleting the first one until there are none left

Code: Select all

While rng.ContentControls.Count > 0
  rng.ContentControls(1).Delete
Wend
Try either of these options inside your story loops.
Andrew Lockton
Melbourne Australia