PDA

View Full Version : [SOLVED:] Replace form checkbox with content control checkbox



vasilinaak
09-05-2022, 03:45 AM
Hi,

I don't have much understanding of MS Word macros, but I am trying to create a macro that would replace all my form checkboxes with content control checkboxes. The newly replaced checkboxes should reflect the old state of the legacy checkboxes. Note that in the document both types of checkboxes can be found and only the legacy ones must be replaced (I have attached an example).
30125
30125
I've found the following code and I tried to adjust it to work for my case, but I keep getting errors:


Sub Checkbox()Dim oRng As Word.Range
Dim oCC As ContentControl
'ChrW(9744) is unchecked box; 9746 is checked box
Set oRng = ActiveDocument.Range
Selection.Find.ClearFormatting
With oRng.Find
.Text = ChrW(9744)
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng)
oCC.Checked = False
oRng.Collapse wdCollapseEnd
oRng.Move wdCharacter, 2
Wend
End With
Set oRng = ActiveDocument.Range
Selection.Find.ClearFormatting
With oRng.Find
.Text = ChrW(9746)
While .Execute
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, oRng)
oCC.Checked = True
oRng.Collapse wdCollapseEnd
oRng.Move wdCharacter, 2
Wend
End With
lbl_Exit:
Exit Sub
End Sub

Any tips or help is highly appreciated!30125

macropod
09-05-2022, 06:38 AM
If your document already has formfields other than checkboxes, you really shouldn't be replacing the formfield checkboxes with content control checkboxes. Formfields and content controls were not designed to be used together in the same document. Trying to do so is a known source of usability problems.

vasilinaak
09-05-2022, 06:45 AM
If your document already has formfields other than checkboxes, you really shouldn't be replacing the formfield checkboxes with content control checkboxes. Formfields and content controls were not designed to be used together in the same document. Trying to do so is a known source of usability problems.

Unfortunately, one of the requests is to replace all checkboxes with content control checkboxes for 6.5k MS Word documents... I've been trying to use a 3rd party automation software for this, but it does not seem to be stable enough and that is why I am trying a Macro solution.

macropod
09-05-2022, 02:44 PM
The point remains, though, that formfields and content controls should not be used in the same document. Whoever dreamt up this requirement needs to understand the usability implications and, moreover, that any macros attached to formfield checkboxes in the documents concerned will cease to function once that change is made.

The code to replace formfield checkboxes with content control checkboxes isn't at all complicated, but I would be reluctant to provide it without those responsible for the change accepting up-front their responsibility for potentially breaking 6.5k MS Word documents...

Chas Kenyon
09-05-2022, 09:22 PM
If the only legacy formfields are the checkboxes, the problem is not there. Paul knows what he is talking about, though. You would want to be getting rid of all legacy formfields, not just the checkboxes.

Also, I can see doing this with templates, but why with documents? They should stay in their current state, not used to create new documents. This is one of those situations where it sounds like the customer is asking for the wrong thing. You should be providing templates with Content Controls based on old documents, not modifying legacy documents.

macropod
09-06-2022, 12:00 AM
If the only legacy formfields are the checkboxes, the problem is not there.
Even if the only legacy formfields are the checkboxes, if they have on-exit macros attached, the change will break whatever checking/unchecking those checkboxes was supposed to do...

You would want to be getting rid of all legacy formfields, not just the checkboxes.
The same goes for any other formfields with the 'calculate on exit' option checked and/or on-exit macros attached.

Chas Kenyon
09-06-2022, 09:09 AM
Even if the only legacy formfields are the checkboxes, if they have on-exit macros attached, the change will break whatever checking/unchecking those checkboxes was supposed to do...

The same goes for any other formfields with the 'calculate on exit' option checked and/or on-exit macros attached.
Agreed.

gmaxey
09-09-2022, 01:32 AM
Sub ConvertFFtoCC()
Dim oFld As FormField
Dim oRng As Range
Dim strName As String, bValue As Boolean
Dim oCC As ContentControl
For Each oFld In ActiveDocument.Range.FormFields
If oFld.Type = 71 Then
Set oRng = oFld.Range
oRng.Select
bValue = oFld.CheckBox.Value
strName = oFld.Name
oFld.Delete
Set oCC = ActiveDocument.ContentControls.Add(8, Selection.Range)
oCC.Checked = bValue
oCC.Title = strName
End If
Next
lbl_Exit:
Exit Sub
End Sub