Consulting

Results 1 to 8 of 8

Thread: Select Content Control DropDown List Item

  1. #1

    Select Content Control DropDown List Item

    Hi,

    I have a drop-down list content control with 3 items in the list:
    1. Death
    2. Illness
    3. 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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,311
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,311
    Location
    Quote Originally Posted by gingerstu View Post
    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
    Last edited by macropod; 05-04-2021 at 12:42 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Quote Originally Posted by macropod View Post
    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!

  6. #6
    Quote Originally Posted by macropod View Post
    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.

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,311
    Location
    Quote Originally Posted by gingerstu View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Quote Originally Posted by macropod View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •