PDA

View Full Version : How to count all empty content controls in doc?



rgmatthes
07-15-2009, 10:41 AM
Hi everyone, I just have a quick question.

I'm trying to count the number of empty content controls in a document. Here's my code:



Dim empty_controls As Integer
empty_controls = 0

For Each content_control In ActiveDocument.ContentControls
If content_control.ShowingPlaceholderText = True Then
empty_controls = empty_controls + 1
End If
Next

MsgBox empty_controls


This code works, but it doesn't account for content controls in the headers and footers. How can I get those included in the total as well?

Thanks! :thumb

Edit: Included version in title. Thanks fumei!

fumei
07-15-2009, 11:32 AM
1. I take it that this is for Word 2007, as I do not know what a contentcontrol is. It is always useful to indicate what version you are asking about.

2. To make sure any code actions through all places in a Word document, you need to loop through the StoryRanges. Headers and footers are a different StoryRange from the MainStory. Look up StoryRanges in Help.

rgmatthes
07-15-2009, 11:59 AM
Looking at StoryRanges now – thank you. I tried the following code, but it's still not working. Any more tips?



Dim empty_controls As Integer
empty_controls = 0

For Each storyrange In ActiveDocument.StoryRanges
storyrange.Select
For Each content_control In Selection.Range.ContentControls
If content_control.ShowingPlaceholderText = True Then
empty_controls = empty_controls + 1
End If
Next
Next

MsgBox empty_controls


I'm probably way off - I'm pretty new to VBA, especially ranges. Any ideas?

rgmatthes
07-15-2009, 12:24 PM
Newly coded, from macropod's work in another thread:



Dim empty_controls As Integer, story_range As Range
empty_controls = 0

For Each story_range In ActiveDocument.StoryRanges
Do
For Each content_control In story_range.ContentControls
If content_control.ShowingPlaceholderText = True Then
empty_controls = empty_controls + 1
End If
Next
Set story_range = story_range.NextStoryRange
Loop Until story_range Is Nothing
Next

MsgBox empty_controls


Still doesn't work, but I learned something: it only searches the story range the user is in when running the macro. For example, say I have a doc with five empty content controls in the body and two in the header. If I run the macro while editing the body, the total returned would be five. If I run the macro while editing the header, the total returned would be two. I need the total returned to be seven. :banghead:

Anyone? Please? :)

rgmatthes
07-15-2009, 12:48 PM
Another update: the above code DOES WORK, but ONLY if all the headers contain text. If you have a section with an empty header, this code will stop search search at that header section and not search the headers of the next section. That's a major problem with me. :(

fumei
07-15-2009, 02:00 PM
Then you need to do a sub-loop of Sections.

"it only searches the story range the user is in when running the macro."

If I understand the code correctly, this should not be correct. The code loops through the storyranges, so the headerfooter storyrange should be covered.

gmaxey
07-18-2009, 06:52 PM
Try:


Sub MainCount()
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim oShp As Shape
Dim lngCCCount As Long
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
lngCCCount = lngCCCount + CountCC(rngStory)
On Error Resume Next
'Check for CCs in Header or Footer shapes
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
lngCCCount = lngCCCount + CountCC(rngStory)
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
MsgBox lngCCCount
End Sub

Function CountCC(ByRef oRng As Word.Range) As Long
Dim empty_controls As Integer
Dim CC As ContentControl
CountCC = 0
For Each CC In oRng.ContentControls
If CC.ShowingPlaceholderText Then CountCC = CountCC + 1
Next CC
End Function

macropod
07-19-2009, 05:28 PM
Hi rgmatthes,

It seems you've stumbled across a bug in Word 2007. Congratulations - for whatever that's worth.

As fumei says, looping through storyranges should work - it does with fields, for example.

My testing has shown the bug to affect footers in like manner also. It doesn't matter what the situation is in any Section but the first - if that section's header is empty, no content controls in subsequent sections will be found. Ditto for footers.

Until MS fixes this bug, a workaround is to ensure there's at least a space character in the first Section's header & footer.

fumei
07-20-2009, 02:25 PM
"It seems you've stumbled across a bug in Word 2007. Congratulations - for whatever that's worth."

:funnyashe

So...macropod, since I do not use 2007, does this apply to all headerfooter objects, not just Primary? Say Primary has a space, but DifferentOddEven and DifferentFirstPage do not, does the bug still happen?

macropod
07-20-2009, 10:49 PM
Hi fumei,

It seems to apply to each headerfooter object independently.

Thus, if the document has a different 1st page, plus different odd/even layout, an empty 1st page headerfooter object doesn't impair Word's ability to find CCs in the other headerfooter objects in the first Section.

Conversely, if the odd/even headerfooter objects in the 1st Section are empty, the CCs in subsequent Sections' odd/even headerfooter objects won't be found.

Oh, and I've learnt that the same flaw applies to shapes too (but not, apparently, to fields).

fumei
07-21-2009, 02:52 PM
oh boy.

fumei
07-21-2009, 02:53 PM
"Conversely, if the odd/even headerfooter objects in the 1st Section are empty, the CCs in subsequent Sections' odd/even headerfooter objects won't be found."

At least it is a consistent bug...