PDA

View Full Version : [SOLVED:] Select Content Control DropDown List Item



gingerstu
05-03-2021, 03:05 PM
Hi,

I have a drop-down list content control with 3 items in the list:

Death
Illness
Death and Illness


My user form allows the user to select which event and the content control should select the corresponding value in the document.

Here's my code:


Dim objCC As ContentControl
Dim objCE As ContentControlListEntry

If ddObjectivesLumpSumBasis.Value = "Death" Then
Set objCC = ActiveDocument.SelectContentControlsByTag("ccObjectivesLumpSumEvent").Item(1)
Set objCE = objCC.DropdownListEntries.Item(1)
objCE.Select
End If


My If Statement runs, but the content control in the document isn't updating.

Thanks in advance!
Stu

macropod
05-03-2021, 04:38 PM
Try:

With ActiveDocument.SelectContentControlsByTag("ccObjectivesLumpSumEvent")(1)
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = ddObjectivesLumpSumBasis.Value Then
.DropdownListEntries(i).Select: Exit For
End If
Next
End With

gingerstu
05-04-2021, 12:06 AM
Thanks Paul, much appreciated. I had to make a couple of tweaks. The item values in my form combobox didn't match the values in the content control dropdown list exactly. they now do.

The above was just a small part of a routine to be ran if a checkbox is checked. The rest of the routine takes two numbers from the user form and enters them into their respective text content controls in the paragraph of text.

heres the full code:



If cbInheritanceLumpSum.Value Then
Dim lumpSumAmount As Range
Set lumpSumAmount = ActiveDocument.SelectContentControlsByTag("ccLumpSum").Item(1).Range
lumpSumAmount.Text = Me.tbLumpSumAmount.Value

Dim lumpSumTerm As Range
Set lumpSumTerm = ActiveDocument.SelectContentControlsByTag("ccObjectivesLumpSumTerm").Item(1).Range
lumpSumTerm.Text = Me.tbLumpSumTerm.Value

With ActiveDocument.SelectContentControlsByTag("ccObjectivesLumpSumEvent")(1)
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = ddObjectivesLumpSumBasis.Value Then
.DropdownListEntries(i).Select: Exit For
End If
Next
End With
End If

Me.Repaint


For the first selection ("death") and the last ("death or suffering a critical illness") all 3 content controls update as expected.

The second option ("suffering a critical illness") updates only 2 out the 3. it updates "ccObjectivesLumpSumTerm" and "ccObjectivesLumpSumEvent" but doesn't update "ccLumpSum".

How strange. Any thoughts on what that might be?

macropod
05-04-2021, 12:21 AM
For the first selection ("death") and the last ("death or suffering a critical illness") all 3 content controls update as expected.

The second option ("suffering a critical illness") updates only 2 out the 3. it updates "ccObjectivesLumpSumTerm" and "ccObjectivesLumpSumEvent" but doesn't update "ccLumpSum".

How strange. Any thoughts on what that might be?
Most likely because something as simple as your content control's title doesn't have the same capitalisation as you've used in the code - or its named differently (e.g. ccLumpSumAmount).

In any event, your code is way too verbose:

If cbInheritanceLumpSum.Value Then
With ActiveDocument
.SelectContentControlsByTag("ccLumpSum")(1).Range.Text = tbLumpSumAmount.Value
.SelectContentControlsByTag("ccObjectivesLumpSumTerm")(1).Range.Text = tbLumpSumTerm.Value
With .SelectContentControlsByTag("ccObjectivesLumpSumEvent")(1)
For i = 1 To .DropdownListEntries.Count
If .DropdownListEntries(i).Text = ddObjectivesLumpSumBasis.Value Then
.DropdownListEntries(i).Select: Exit For
End If
Next
End With
End With
End If

gingerstu
05-04-2021, 12:34 AM
In any event, your code is way too verbose:


I'm not surprised - I'm not a developer and I'm coming at VBA for the first time.

Thank you for the simplification. I've literally collating fragments of code from around the Internet to shoe horn it to my purposes. It's working thus far, but I don't doubt there will be ways to clean up the code

A little knowledge can be a dangerous thing!

gingerstu
05-04-2021, 12:54 AM
Most likely because something as simple as your content control's title doesn't have the same capitalisation as you've used in the code - or its named differently (e.g. ccLumpSumAmount).



You'll probably laugh at this, but the problem was....

I had a section at the end of my document that contained a "repository" of all the building blocks I'd created. I have a book mark where I append a number of building blocks depending on check boxes selected that contain the content controls we've been discussing.

Where the data was missing from the buildings blocks programmatically added, the data appeared in the content controls in the building blocks repository section of my document.

The code was working fine, it just couldn't make up it's mind which content control to update. I've now removed this "repository" section from the template and saved it in an autonomous document.

The template now works beautifully.

Every day is a learning day.

macropod
05-04-2021, 01:13 AM
Where the data was missing from the buildings blocks programmatically added, the data appeared in the content controls in the building blocks repository section of my document.

The code was working fine, it just couldn't make up it's mind which content control to update. I've now removed this "repository" section from the template and saved it in an autonomous document.
No, it knew exactly which content control to update - the one with the '1' index, which just happened to be the one in the "repository". Content control index #s are unrelated to their position in the document.

gingerstu
05-04-2021, 03:32 AM
No, it knew exactly which content control to update - the one with the '1' index, which just happened to be the one in the "repository". Content control index #s are unrelated to their position in the document.

Well that makes perfect sense. If you create a building block that contains a content control, will the index get updated automatically if there is more than one content control of the same type and name in the document?

My next challange is to look at some repeating tables that contain content controls. My use case is an insurance suitability letter for life insurance. We can sell a single policy but it may have more than one coverage. my repeater content repeats the coverage part of the "master" table containing content controls for coverage type, sum assured, term etc.

I was going to leave this for manual editing once my UserForm has handled the majority of the other data entry. But, if the index of content controls with the same type and name update automatically, presumably I can set some code to loop through the content controls.

I'd have to have somesort of field on the user form to say how many policies have been sold, and then for each policy sold how many coverages it contains. On submit the script would add the required building blocks to the document.

A new userform would appear for each policy and associated coverage and update based on the correct index #.

That sounds complicated! I might just leave it the user to manipulate the tables and content controls in the doc.