Consulting

Results 1 to 3 of 3

Thread: VBA Working with Form Fields and Content Controls

  1. #1
    VBAX Newbie
    Joined
    Feb 2015
    Posts
    4
    Location

    Question VBA Working with Form Fields and Content Controls

    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.

  2. #2
    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
    Last edited by gmayor; 03-02-2015 at 11:51 PM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Feb 2015
    Posts
    4
    Location
    Thank you so much, this is exactly what I was looking for to convert drop down form fields to content controls!

Posting Permissions

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