none
wDoc.ContentControls does not include content controls in shapes RRS feed

  • Question

  • Hello,

    I am trying to loop through all the content controls in word document, bud method wDoc.ContentControls does not return content controls in shapes (textbox ...)

    However wDoc.SelectContentControlsByTitle() works fine for such content controls

    What is the right way to retrieve all the content controls in a word document, including the ones in shapes?

    Thank you,

    Jakub

    Thursday, December 14, 2017 1:40 PM

Answers

  • Hello,

    The issue does exist. I test in Office 2010 and Office 2016.  ActiveDocument.StoryRanges(wdTextFrameStory) could only get the first shape.

    I think we are unable to get all content controls using one method. You may use the following code to work around.

    Dim cc As ContentControl
    Dim sh As Shape
    
    For Each cc In ActiveDocument.ContentControls
    Debug.Print cc.Title
    Next
    
    For Each sh In ActiveDocument.Shapes
    For Each cc In sh.TextFrame.TextRange.ContentControls
    Debug.Print cc.Title
    Next
    Next

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jakub dusek Thursday, December 21, 2017 1:02 PM
    Tuesday, December 19, 2017 3:30 AM
    Moderator

All replies

  • Hello,

    Here is the VBA code to loop through all content conrtols.

    Sub loopThroughAllCC()
        Dim cc As ContentControl
        Dim rng As Range
        For Each rng In ActiveDocument.StoryRanges
            For Each cc In rng.ContentControls
            Debug.Print cc.Title
            Next
        Next
    End Sub

    Reference:https://stackoverflow.com/questions/4605179/how-to-get-the-list-of-all-content-controls-in-the-document

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 15, 2017 2:29 AM
    Moderator
  • Dear Celeste,

    thank you for your answer. The code lists content controls in word textboxes - great! -, but still misses content controls in shapes.

    For example:

    The suggested code returns only this output:

    regular
    textbox

    Any ideas how to include really all content controls?

    Friday, December 15, 2017 12:21 PM
  • Hello,

    How do you add the shape and the content control?

    When i add a shape, right click to add text then add a content control, change the title of the content control. The code above could return the expected result.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 18, 2017 2:43 AM
    Moderator
  • Hello,

    Yes that is how I add shapes and content controls.

    I found out that if there is only one content control in a document, and it is in shape, the code works fine.

    Bud when I add more content controls, usually not all are listed by the code.

    I should mention that i am using Word 2010.

    For example this:


    For this setup the code returns:

    1
    3
    2

    I uploaded it here:

    https://drive.google.com/open?id=1zykMnUNpu2QNsrqmW30thl5-CWvxg2A7

    Could you try it on your PC please?

    Monday, December 18, 2017 12:24 PM
  • Hello,

    The issue does exist. I test in Office 2010 and Office 2016.  ActiveDocument.StoryRanges(wdTextFrameStory) could only get the first shape.

    I think we are unable to get all content controls using one method. You may use the following code to work around.

    Dim cc As ContentControl
    Dim sh As Shape
    
    For Each cc In ActiveDocument.ContentControls
    Debug.Print cc.Title
    Next
    
    For Each sh In ActiveDocument.Shapes
    For Each cc In sh.TextFrame.TextRange.ContentControls
    Debug.Print cc.Title
    Next
    Next

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jakub dusek Thursday, December 21, 2017 1:02 PM
    Tuesday, December 19, 2017 3:30 AM
    Moderator
  • Thank you!
    Thursday, December 21, 2017 1:02 PM