PDA

View Full Version : [SOLVED:] VBA Working with Form Fields and Content Controls



calbs
02-27-2015, 11:12 AM
Is there a VBA code that will allow me to get the list of values in a "legacy form field" drop-down?

I want to write a macro that takes the old drop down form field and converts it into a new content control in Word 2013 with the list of values already typed into it.

What we have are old documents that have legacy form fields, and we want to convert them to have new content controls that allow rich text editing (for bolding, underlining, etc.).

The problem is if we only convert the text form fields, and leave the checkboxes and drop-downs as legacy, it doesn't play nicely when the document is set to read only (with exceptions for the content controls). In read only, the drop-downs won't drop as it does in Form Filling protection.

We can't change to Form Filling protection because it causes problems with new rich text controls in that tab actually tabs new spacing instead of jumping to the next editable field.

If I convert everything to the new content controls it works alright in read-only with content control exceptions, but I need some code which will include the old list of values from a legacy drop-down in the new drop-down control.

gmayor
03-02-2015, 11:36 PM
The following will replace the dropdown formfields in a document with dropdown content controls.



Option Explicit

Sub ReplaceFormFields()
Dim oFF As FormField
Dim bProtected As Boolean
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
For Each oFF In ActiveDocument.FormFields
If oFF.Type = wdFieldFormDropDown Then
ReplaceDropDownFF oFF.name
End If
Next oFF
lbl_Exit:
Exit Sub
End Sub

Sub ReplaceDropDownFF(strFieldName As String)
Dim oFF As FormField
Dim oCC As ContentControl
Dim oRng As Range
Dim strList As String
Dim vList As Variant
Dim i As Long
strList = ""
Set oFF = ActiveDocument.FormFields(strFieldName)
For i = 1 To oFF.Dropdown.ListEntries.Count
strList = strList & oFF.Dropdown.ListEntries(i).name
If i < oFF.Dropdown.ListEntries.Count Then
strList = strList & "|"
End If
Next i
vList = Split(strList, "|")
Set oRng = oFF.Range
oRng.Delete
Set oCC = oRng.ContentControls.Add(wdContentControlDropdownList, oRng)
oCC.Title = strFieldName
For i = 0 To UBound(vList)
oCC.DropdownListEntries.Add vList(i)
Next i
lbl_Exit:
Exit Sub
End Sub

calbs
03-03-2015, 10:34 AM
Thank you so much, this is exactly what I was looking for to convert drop down form fields to content controls!