Results 1 to 3 of 3

Thread: Solved: VBA code to set Drop-Down content control to first value

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Posts
    71
    Location

    Solved: VBA code to set Drop-Down content control to first value

    I have a word document that started out with legacy form fields that would fire macros to pull up a drop down list. This got around the limitation to the number of items you could have in a legacy drop down. I then created a reset button that would fire a macro to clear all of the form fields (and various check boxes as you will see in the code below).

    I now want to switch to new style drop-down content control (which have no limit to the number of items) so I don't have macros that need to fire to fill out the form (a security concern to some). I cannot seem to come up with the proper code for my reset button to reset all of the drop-down content controls to a default value (usually the first item in the list).

    Here is the code I have:

    [vba]Private Sub ResetButton_Click()
    Dim bProtected As Boolean
    Dim oFld As FormFields
    Dim oControl As ContentControls
    Dim oCtrl As ContentControl
    Dim oList As ContentControlListEntries
    Dim objMap As XMLMapping
    Dim i As Long
    Dim j As Long
    Set oFld = ActiveDocument.FormFields
    Set oControl = ActiveDocument.ContentControls
    'Unprotect the file
    If ActiveDocument.ProtectionType <> wdNoProtection Then
    bProtected = True
    ActiveDocument.Unprotect Password:="xxxxxx"
    End If

    'code to clear old style form fields - this works
    For i = 1 To oFld.Count
    With oFld(i)
    .Select
    If .Name <> "" Then
    Dialogs(wdDialogFormFieldOptions).Execute
    End If
    End With
    Next

    'attempted code to reset Drop Down content control to first value in list - this does not work
    For j = 1 To oControl.Count
    With oControl(j)
    Set oList = oCtrl.DropdownListEntries.Item(1)
    oList.Select


    End With
    Next

    'Reprotect the document.
    If bProtected = True Then
    ActiveDocument.Protect _
    Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="xxxxxx"
    End If

    'code to clear various check boxes
    CheckBox1.Value = 0
    CheckBox2.Value = 0
    CheckBox3.Value = 0
    CheckBox4.Value = 0
    CheckBox5.Value = 0
    CheckBox6.Value = 0
    CheckBox7.Value = 0
    CheckBox8.Value = 0
    CheckBox9.Value = 0
    CheckBox10.Value = 0
    CheckBox11.Value = 0
    CheckBox12.Value = 0
    CheckBox13.Value = 0
    oFld(1).Select
    End Sub
    [/vba]

    Any help would be greatly appreciated.

    Thank you,

    Brian

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    [VBA]Private Sub SetCCDropDownListEntry()
    Dim oControls As ContentControls
    Dim oLE As ContentControlListEntry
    Set oControls = ActiveDocument.ContentControls
    Dim j As Long
    For j = 1 To oControls.Count
    If oControls(j).Type = wdContentControlDropdownList Then
    Set oLE = oControls(j).DropdownListEntries(1)
    oLE.Select
    End If
    Next
    End Sub
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Dec 2006
    Posts
    71
    Location
    Greg,

    That worked perfectly. I knew I was close but was missing something.

    I appreciate the help.

Posting Permissions

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