PDA

View Full Version : Replace Form Field Checkbox with Content Control Checkbox



calvertnr
02-26-2018, 08:44 AM
Word 2016

I need to replace the legacy form field checkboxes with content control checkboxes.

I have never written any macros, please excuse my ignorance. We have old checklists that I want to update. I have my feeble attempt at code below. Example document is attached. There may be a better way to fix this.

Sub ChkBxTab()
Dim i As Long
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
Set Rng = .Range
.Delete
Rng = wdContentControlCheckBox
End If
End With
Next
End With
End Sub

Any help is appreciated!

macropod
02-26-2018, 03:05 PM
Your document already contains text formfields, and formfields and content controls should not be used in the same document; doing so can lead to problems - especially when checkboxes are involved. In any event, having Yes/No/NA checkboxes is only asking for problems; you should consider using a single dropdown with Yes/No/NA options for each item, so only one answer per item can be given.

The following macro will replace your text formfields with plain text content controls and your checkbox sets with dropdown content controls:


Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, Rng As Range, CCtrl As ContentControl
With ActiveDocument
While .FormFields.Count > 0
Set Rng = .FormFields(.FormFields.Count).Range
With Rng
Select Case .FormFields(1).Type
Case wdFieldFormTextInput
.Text = vbNullString
Set CCtrl = .ContentControls.Add(wdContentControlText)
CCtrl.SetPlaceholderText Text:="Type Here"
Case wdFieldFormCheckBox
.MoveStartUntil vbCr, wdBackward
.Text = vbNullString
Set CCtrl = .ContentControls.Add(wdContentControlDropdownList)
CCtrl.SetPlaceholderText Text:="Select one"
With CCtrl.DropdownListEntries
.Add "Yes"
.Add "No"
.Add "N/A"
End With
End Select
End With
Wend
End With
Application.ScreenUpdating = True
End Sub
You can see the processing result in the attached.

gmaxey
02-26-2018, 08:18 PM
My Content Control Tools Add-in has a utility for converting FFs to CCs:

https://gregmaxey.com/word_tip_pages/content_control_tools.html