Get ContentControl by title (or tag)

泪湿孤枕 提交于 2019-12-12 20:24:25

问题


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.)


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/46009985/get-contentcontrol-by-title-or-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!