Content Control help please

B

Bill

Is it possible for the selection of a certain value in a content control drop
down list can either (1) cause a second CC drop down list to select a defined
value or (2) cause text to be inserted at a bookmark in a specified location?

I have two CC drop downs. First is "Name" and has over 50 values, the
second is "Number" which has less than 10 values. Each "Name" corresponds
with a certain "Number," and obviously each number correspondes with several
"Names". What I'm looking for is the user select the appropriate "Name" and
the appropriate "Number" is automatically selected. Is this possible?

My backup is something I have more experience with from my limited
background using UserForms. That would be to have the user select the "Name"
and then the Number is inserted as text at a bookmark instead of in a CC.

I appreciate any help I can get in solving this. Thanks
 
G

Greg Maxey

Both are possible as well as a third option of have a plain text CC
titled "Number"

Document contains:

A dropdown CC titled "Name" with a list of names
A dropdown CC titled "Number" with Select a number and 1 through 10 as
list entries
A plain text CC titled "Number"
A bookmark titled "Number"

Use the following code (adapt the Case statements as required):

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl,
Cancel As Boolean)
Select Case CC.Title
Case "Name"
Select Case CC.Range.Text
Case "Bill", "Joe", "Mary"
SetCCDropDownValue 1
SetCCPlainTextValue "1"
WriteToBM "One"
Case "Cindy", "Susan", "John"
SetCCDropDownValue 2
SetCCPlainTextValue "2"
WriteToBM "Two"
End Select
Case Else
'Do Nothing
End Select
End Sub
Sub SetCCDropDownValue(ByRef pLng As Long)
ActiveDocument.SelectContentControlsByTitle("Number").Item
(1).DropdownListEntries(pLng + 1).Select
End Sub
Sub SetCCPlainTextValue(ByRef pStr As String)
ActiveDocument.SelectContentControlsByTitle("Number").Item
(2).Range.Text = pStr
End Sub
Sub WriteToBM(ByRef pStr As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Number").Range
oRng.Text = pStr
ActiveDocument.Bookmarks.Add "Number", oRng
End Sub
 
J

Jay Freedman

Is it possible for the selection of a certain value in a content control drop
down list can either (1) cause a second CC drop down list to select a defined
value or (2) cause text to be inserted at a bookmark in a specified location?

I have two CC drop downs. First is "Name" and has over 50 values, the
second is "Number" which has less than 10 values. Each "Name" corresponds
with a certain "Number," and obviously each number correspondes with several
"Names". What I'm looking for is the user select the appropriate "Name" and
the appropriate "Number" is automatically selected. Is this possible?

My backup is something I have more experience with from my limited
background using UserForms. That would be to have the user select the "Name"
and then the Number is inserted as text at a bookmark instead of in a CC.

I appreciate any help I can get in solving this. Thanks

Yes, it's possible, although not entirely straightforward.

Put in a Dropdown content control and a Plain Text content control (not a second
dropdown). In the Properties dialogs of the two controls, enter a Tag value for
each -- I'll suggest NameList and NumberResult, although they could be anything,
and if you use something different you'll have to adjust the macro accordingly.

In the Properties dialog of the Dropdown control, add entries (or modify the
existing entries) to put the name into the Display Name and the corresponding
number into the Value. You probably want to check the box for "Content control
cannot be deleted".

In the Properties dialog of the Plain Text control, check the two boxes for
"Content control cannot be deleted" and "Contents cannot be edited". That way,
the number that appears there will come only from the list in the dropdown, and
users can't just click in the control and change it.

Put this macro in the ThisDocument module of the template:

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As
Boolean)
Dim idx As Integer
Dim TB As ContentControl

' Do work only if the cursor just exited the NameList dropdown
If (LCase(CC.Tag) = "namelist") Then
Set TB = SelectContentControlsByTag("NumberResult")(1)
If CC.ShowingPlaceholderText Then
With TB
' Because the text control is locked, unlock it
' just long enough to write to it.
.LockContents = False
.Range.Text = ""
.LockContents = True
End With
Else
' Find which entry the user selected
For idx = 2 To CC.DropdownListEntries.Count
If CC.DropdownListEntries(idx).Text = CC.Range.Text Then
Exit For
End If
Next

' On exit from the loop, idx is the index of
' the selected item. Write its Value to the text control.
With TB
.LockContents = False
.Range.Text = CC.DropdownListEntries(idx).Value
.LockContents = True
End With
End If
End If
End Sub

The content control in the CC variable is the one the cursor is leaving. The
CC.Range.Text property is whatever is showing in the control. The macro first
checks that the control is the right one (by comparing the Tag). If a selection
has not been made -- the placeholder text is showing -- then the macro clears
the text control.

If there is a selection, the macro compares the visible text to the Text part of
each entry in the list until it finds the one that matches. It then assigns the
Value part of that entry to the text control.
 
B

Bill

"In the Properties dialog of the Dropdown control, ... put the name into the
Display Name and the corresponding number into the Value."

I'm not able to have multiple instances of the same Value.

For my purposes There are 66 names, and 7 numbers, so each number, or value,
would have to apply to several names.
 
B

Bill

Thanks, it took me a couple tries to get it to work right but then I saw the
"pLng + 1" was throwing the numbering off by one... good to go now.... works
just like I wanted it to.
 
J

Jay Freedman

Sorry, I didn't follow up that part. It would have been slick if Microsoft
wasn't so restrictive (and I don't see what their reason for it would be).
I'm glad to see you got Greg's solution working.
 
G

Greg Maxey

Bill,

pLng + 1 was to accomodate the "placeholder text" in the second DD. You
must not be using it.
 

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