PDA

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



g8r777
07-14-2011, 04:09 PM
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:

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


Any help would be greatly appreciated.

Thank you,

Brian

gmaxey
07-14-2011, 06:47 PM
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

g8r777
07-15-2011, 07:31 AM
Greg,

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

I appreciate the help.