问题
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