Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ContentControl by title (or tag)

Tags:

ms-word

vba

Document.ContentControls collection doesn't support item retrieval by name, only by index.

Can I still get a specific ContentControl by a user-defined identifier so as to keep code readable? (e.g. Content control titles - Ms Office Forums claims it's only possible to try them one by one.)

like image 471
ivan_pozdeev Avatar asked Dec 18 '22 04:12

ivan_pozdeev


2 Answers

There are Document.SelectContentControlsByTitle() and Document.SelectContentControlsByTag() methods for this.

Since neither property of a control is guaranteed to be unique, both return a ContentControls collection of results. A function like this can be used to verify that the result exists and is unique:

Public Function CCSingle(source As ContentControls) As ContentControl
    Select Case Sgn(source.Count - 1)
    Case -1
        '9 = subscript out of range
        'http://onlinelibrary.wiley.com/doi/10.1002/9781118257616.app3/pdf
        Call Err.Raise(9, , "Identifier not found")
    Case 1
        Call Err.Raise(9, , "Identifier not unique")
    Case Else
        Set CCSingle = source.Item(1)
    End Select
End Function
like image 50
ivan_pozdeev Avatar answered Dec 20 '22 18:12

ivan_pozdeev


Content Controls can be identified by their .Tag property and their .Title property. Here is something simplistic. It returns the first Content Control from the set of all Content Controls that matches both a Title and Tag.

Function FindCCbyTitleAndTag (Title as string, Tag as string) as ContentControl
  Dim CC as ContentControl
  For each CC in ActiveDocument.ContentControls
    If CC.Title = Title and CC.Tag = Tag then
      FindCCbyTitleAndTag = CC
    End If
  Next CC
End Function
like image 39
Bruce M Avatar answered Dec 20 '22 17:12

Bruce M