none
Searching for a Content Control via Title or Tag using a Wildcard RRS feed

  • Question

  • I have a Word 2010 document with a number of content controls, some of which have Titles and Tags like "PurchasePriceAmendment" or "RevenueShortfallAmendment". Other content controls have total different names that don't include the word "Amendment". I want to write a macro that works only on the content controls that have "Amendment" in their Title/Tag. I know how to find content controls with particular Titles/Tags but I am having problems figuring out if it is possible to pick ones out with similar titles/Tags. 

    This doesn't work:

         If CC.Title like "*Amendment" then
              debug.print CC.Title

         End If

    I also saw a post about using "Contains" but that was for a cell in Excel and I got an error message when I tried to do it in Word VBA.

    I saw a post about grouping Content Controls.  Would that help in any way?  And how do I refer to grouped content controls?

    Thanks in advance for any help you can give.  Carol.

    Tuesday, February 24, 2015 3:19 PM

Answers

  • This doesn't work:

         If CC.Title like "*Amendment" then
              debug.print CC.Title

         End If

    Hi cpnc,

    I'm not sure what the complete method you used to find the Content Control, but the Like operator and the wildcard characters work fine on my machine(Win7 with Office 2010).

    Wildcard Characters used in String Comparisons

    This is the test code, I use the first method to insert content controls with titles, then succesfully use the second method to find all the content controls whose title match the pattern use the wild card characters:

    Sub InsertContentControls()
        Dim cc As ContentControl
        Dim objDate As Date
     
        Set cc = ActiveDocument.ContentControls _
            .Add(wdContentControlText)
            
        cc.Title = "RevenueShortfallAmendment"
        'objDate = Date
        cc.Range.Text = "abc"
    End Sub
    
    Sub FindContentControlByTitle()
        Dim i As Integer
        Dim cc As ContentControl
        For i = 1 To ThisDocument.ContentControls.Count
            Set cc = ThisDocument.ContentControls(i)
            If cc.Title Like "*Amendment" Then
                Debug.Print cc.Title
            End If
        Next i
    End Sub
    
    
    

    Another way to find a string with a specified pattern is to utilize the Regular Expression in VBA:

    https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

    In the VBE, add reference to "Microsoft VBScript Regular Expressions" then you can use Regular Expressions. Search "vba regular expression" in your favorite search engine, and you'll find tutorials about how to use Regular Expressions in VBA.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by cpnc Wednesday, February 25, 2015 1:43 PM
    Wednesday, February 25, 2015 8:05 AM
    Moderator

All replies

  • This doesn't work:

         If CC.Title like "*Amendment" then
              debug.print CC.Title

         End If

    Hi cpnc,

    I'm not sure what the complete method you used to find the Content Control, but the Like operator and the wildcard characters work fine on my machine(Win7 with Office 2010).

    Wildcard Characters used in String Comparisons

    This is the test code, I use the first method to insert content controls with titles, then succesfully use the second method to find all the content controls whose title match the pattern use the wild card characters:

    Sub InsertContentControls()
        Dim cc As ContentControl
        Dim objDate As Date
     
        Set cc = ActiveDocument.ContentControls _
            .Add(wdContentControlText)
            
        cc.Title = "RevenueShortfallAmendment"
        'objDate = Date
        cc.Range.Text = "abc"
    End Sub
    
    Sub FindContentControlByTitle()
        Dim i As Integer
        Dim cc As ContentControl
        For i = 1 To ThisDocument.ContentControls.Count
            Set cc = ThisDocument.ContentControls(i)
            If cc.Title Like "*Amendment" Then
                Debug.Print cc.Title
            End If
        Next i
    End Sub
    
    
    

    Another way to find a string with a specified pattern is to utilize the Regular Expression in VBA:

    https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

    In the VBE, add reference to "Microsoft VBScript Regular Expressions" then you can use Regular Expressions. Search "vba regular expression" in your favorite search engine, and you'll find tutorials about how to use Regular Expressions in VBA.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by cpnc Wednesday, February 25, 2015 1:43 PM
    Wednesday, February 25, 2015 8:05 AM
    Moderator
  • Thank you Caillen.  Turns out this was user error all the way.  My Content Controls had the word "Amendment" in the TAG but I was searching for it in the TITLE.  duhhhhhhh!

    Carol.

    Wednesday, February 25, 2015 1:43 PM