Discussion:
Accessing ContentControls via its Title?
(too old to reply)
d***@gmail.com
2007-05-21 20:57:43 UTC
Permalink
Hello

Exists there a VBA code possiblity to access a specific content
control via its title rather than its index?
i.e.

Application.ActiveDocument.ContentControls(1) --> works !
Application.ActiveDocument.ContentControls("MyDropdown") --> ERROR
Application.ActiveDocument.ContentControls.Item("MyDropdown") -->
ERROR

Any other suggestions?

thanks in advance
regards
data
Doug Robbins - Word MVP
2007-05-22 08:23:43 UTC
Permalink
You can access them uing the title by means of the following routine (note
however that the title is case sensitive)

Dim i As Long
With ActiveDocument
For i = 1 To .ContentControls.Count
If .ContentControls(i).Title = "MyDropdown" Then
'Do what you want with the contentcontrol
Exit For
End If
Next
End With
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
Post by d***@gmail.com
Hello
Exists there a VBA code possiblity to access a specific content
control via its title rather than its index?
i.e.
Application.ActiveDocument.ContentControls(1) --> works !
Application.ActiveDocument.ContentControls("MyDropdown") --> ERROR
Application.ActiveDocument.ContentControls.Item("MyDropdown") -->
ERROR
Any other suggestions?
thanks in advance
regards
data
d***@gmail.com
2007-05-23 20:31:53 UTC
Permalink
Hello Robbins

Thx for your answer. So if it is not possible to access it via its
title directly I coded a class module which can be simple used as
follows



1. Create a new class module with the name
2. copy paste the class module code
3. create a module with a test method and inserts the example
4. run your macro

....cheers ....
-------------------------------------------------------------------------------
CLASS MODULE:

Option Explicit
Private dictContentControls
Private Sub Class_Initialize()
Dim i As Integer
Dim c As Collection

Set dictContentControls = CreateObject("Scripting.Dictionary")

For i = 1 To ActiveDocument.ContentControls.Count
If Not
dictContentControls.Exists(ActiveDocument.ContentControls.Item(i).title)
Then
Set c = New Collection
dictContentControls.Add
ActiveDocument.ContentControls.Item(i).title, c
Else
Set c =
getControls(ActiveDocument.ContentControls.Item(i).title)
End If
c.Add ActiveDocument.ContentControls.Item(i)
Next
End Sub
Public Function getControl(title) As ContentControl
Dim c As Collection
Set c = dictContentControls.Item(title)
Set getControl = c.Item(1)
End Function
Public Function getControls(Optional title) As Collection
If Not IsMissing(title) Then
Set getControls = dictContentControls.Item(title)
Else
Set getControls = getAllControls()
End If
End Function
Public Function getAllControls() As Collection
Dim c
Dim ctrl As ContentControl
Set getAllControls = New Collection
For Each c In dictContentControls.Items()
For Each ctrl In c
getAllControls.Add ctrl
Next
Next
End Function
--------------------------------------------------------------------------------------------
EXAMPLE MACRO

Private Sub Document_Open()

Dim cm As ContentControlsManager
Set cm = New ContentControlsManager

Dim c As Collection
Dim ctrl As ContentControl

Set ctrl = cm.getControl("MyDropdown")

ctrl.DropdownListEntries.Clear
ctrl.DropdownListEntries.Add "Red", 1
ctrl.DropdownListEntries.Add "Blue", 2

'get All Controls
Set c = cm.getControls()
For each ctrl in c
MsgBox ctrl.title
Next

End Sub
Post by Doug Robbins - Word MVP
You can access them uing the title by means of the following routine (note
however that the title is case sensitive)
Dim i As Long
With ActiveDocument
For i = 1 To .ContentControls.Count
If .ContentControls(i).Title = "MyDropdown" Then
'Do what you want with the contentcontrol
Exit For
End If
Next
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
Post by d***@gmail.com
Hello
Exists there a VBA code possiblity to access a specific content
control via its title rather than its index?
i.e.
Application.ActiveDocument.ContentControls(1) --> works !
Application.ActiveDocument.ContentControls("MyDropdown") --> ERROR
Application.ActiveDocument.ContentControls.Item("MyDropdown") -->
ERROR
Any other suggestions?
thanks in advance
regards
data
Doug Robbins - Word MVP
2007-05-24 09:05:01 UTC
Permalink
Thanks for sharing.
--
Regards,

Doug Robbins - Word MVP
Post by d***@gmail.com
Hello Robbins
Thx for your answer. So if it is not possible to access it via its
title directly I coded a class module which can be simple used as
follows
1. Create a new class module with the name
2. copy paste the class module code
3. create a module with a test method and inserts the example
4. run your macro
....cheers ....
-------------------------------------------------------------------------------
Option Explicit
Private dictContentControls
Private Sub Class_Initialize()
Dim i As Integer
Dim c As Collection
Set dictContentControls = CreateObject("Scripting.Dictionary")
For i = 1 To ActiveDocument.ContentControls.Count
If Not
dictContentControls.Exists(ActiveDocument.ContentControls.Item(i).title)
Then
Set c = New Collection
dictContentControls.Add
ActiveDocument.ContentControls.Item(i).title, c
Else
Set c =
getControls(ActiveDocument.ContentControls.Item(i).title)
End If
c.Add ActiveDocument.ContentControls.Item(i)
Next
End Sub
Public Function getControl(title) As ContentControl
Dim c As Collection
Set c = dictContentControls.Item(title)
Set getControl = c.Item(1)
End Function
Public Function getControls(Optional title) As Collection
If Not IsMissing(title) Then
Set getControls = dictContentControls.Item(title)
Else
Set getControls = getAllControls()
End If
End Function
Public Function getAllControls() As Collection
Dim c
Dim ctrl As ContentControl
Set getAllControls = New Collection
For Each c In dictContentControls.Items()
For Each ctrl In c
getAllControls.Add ctrl
Next
Next
End Function
--------------------------------------------------------------------------------------------
EXAMPLE MACRO
Private Sub Document_Open()
Dim cm As ContentControlsManager
Set cm = New ContentControlsManager
Dim c As Collection
Dim ctrl As ContentControl
Set ctrl = cm.getControl("MyDropdown")
ctrl.DropdownListEntries.Clear
ctrl.DropdownListEntries.Add "Red", 1
ctrl.DropdownListEntries.Add "Blue", 2
'get All Controls
Set c = cm.getControls()
For each ctrl in c
MsgBox ctrl.title
Next
End Sub
Post by Doug Robbins - Word MVP
You can access them uing the title by means of the following routine (note
however that the title is case sensitive)
Dim i As Long
With ActiveDocument
For i = 1 To .ContentControls.Count
If .ContentControls(i).Title = "MyDropdown" Then
'Do what you want with the contentcontrol
Exit For
End If
Next
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
Post by d***@gmail.com
Hello
Exists there a VBA code possiblity to access a specific content
control via its title rather than its index?
i.e.
Application.ActiveDocument.ContentControls(1) --> works !
Application.ActiveDocument.ContentControls("MyDropdown") --> ERROR
Application.ActiveDocument.ContentControls.Item("MyDropdown") -->
ERROR
Any other suggestions?
thanks in advance
regards
data
Loading...