Accessing ContentControls via its Title?

D

data.deutrium

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

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
 
D

data.deutrium

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top