View Full Version : [SOLVED:] Replacing checkboxes with text
Cabaq
08-26-2015, 01:33 AM
Hi!
I'd like to replace checkboxes in a document with one word if it's checked, and with another word if it's unchecked. Anybody know if there's a way to do this?
Thanks
gmayor
08-26-2015, 01:52 AM
If the check boxes are content control check boxes then you could use setup an exit event by using the following code in the ThisDocument module of the document and save the document as macro enabled
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal ContentControl _
                                          As ContentControl, _
                                          Cancel As Boolean)
Dim orng As Range
    If ContentControl.Type = wdContentControlCheckBox Then
        Set orng = ContentControl.Range
        If ContentControl.Checked = True Then
            ContentControl.LockContentControl = False
            ContentControl.LockContents = False
            ContentControl.Delete
            orng.Text = "True Text"
        Else
            ContentControl.LockContentControl = False
            ContentControl.LockContents = False
            ContentControl.Delete
            orng.Text = "False Text"
        End If
        orng.Font.Name = "Calibri"
    End If
lbl_Exit:
    Exit Sub
End Sub
For other types of check box you will need to provide more detail.
Cabaq
08-26-2015, 03:40 AM
If the check boxes are content control check boxes then you could use setup an exit event by using the following code in the ThisDocument module of the document and save the document as macro enabled
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal ContentControl _
                                          As ContentControl, _
                                          Cancel As Boolean)
Dim orng As Range
    If ContentControl.Type = wdContentControlCheckBox Then
        Set orng = ContentControl.Range
        If ContentControl.Checked = True Then
            ContentControl.LockContentControl = False
            ContentControl.LockContents = False
            ContentControl.Delete
            orng.Text = "True Text"
        Else
            ContentControl.LockContentControl = False
            ContentControl.LockContents = False
            ContentControl.Delete
            orng.Text = "False Text"
        End If
        orng.Font.Name = "Calibri"
    End If
lbl_Exit:
    Exit Sub
End Sub
For other types of check box you will need to provide more detail.
Thanks for your fast reply. I think the type is wdFieldFormCheckBox since the code below works (but doesn't really do what i want)
Sub ChkBxClr() 
    Dim i As Long, Rng As Range 
    With ActiveDocument 
        For i = .FormFields.Count To 1 Step -1 
            With .FormFields(i) 
                If .Type = wdFieldFormCheckBox Then 
                    Set Rng = .Range 
                    .Delete 
                    Rng.Text = "[__]" 
                End If 
            End With 
        Next 
        Set Rng = Nothing 
    End With 
End Sub
gmayor
08-26-2015, 04:46 AM
Your macro must unprotect the form in order to work and then it will replace all checkboxes with the text string e.g.
Sub ChkBxClr()
Dim i As Long, Rng As Range
Dim bProtected As Boolean
    'Unprotect the file
    If Not ActiveDocument.ProtectionType = wdNoProtection Then
        bProtected = True
        ActiveDocument.Unprotect Password:=""
    End If
    With ActiveDocument
        For i = .FormFields.Count To 1 Step -1
            With .FormFields(i)
                If .Type = wdFieldFormCheckBox Then
                    Set Rng = .Range
                    .Delete
                    Rng.Text = "[__]"
                End If
            End With
        Next
        Set Rng = Nothing
    End With
    'Reprotect the document.
    If bProtected = True Then
        ActiveDocument.Protect _
                Type:=wdAllowOnlyFormFields, _
                NoReset:=True, _
                Password:=""
    End If
End Subbut that doesn't appear to be what you originally asked?
Cabaq
08-26-2015, 05:55 AM
My macro changes all the checkboxes to "[___]". I've tried it and it works in that task. I would just like it to change a checked box into one text string, and an unchecked box to another. Just like in your code, but so it works with my type of checkbox (wdFieldFormCheckBox).
gmayor
08-26-2015, 10:21 PM
It is rather more complicated to achieve with legacy form fields. For that you need the following code. Set each formfield to run FFOnEntry on Entry to the field and FFOnExit on exit from the field. The macro at the end will add the macros to the fields. Test with a copy of the document as the process is destructive of form fields.
Option Explicit
Private rngFF As Word.Range
Private fldFF As Word.FormField
Private mstrFF As String
Private bProtected As Boolean
Private bValue As Boolean
Private oRng As Word.Range
Private strCurrentFF As String
Private ofld As FormField
Public Sub FFOnExit()
    With GetCurrentFF
        If GetCurrentFF.Type = wdFieldFormCheckBox Then
            'Unprotect the file
            If Not ActiveDocument.ProtectionType = wdNoProtection Then
                bProtected = True
                ActiveDocument.Unprotect Password:=""
            End If
            Set oRng = .Range
            mstrFF = GetCurrentFF.name
            bValue = .CheckBox.Value
            .Range.Delete
            If bValue = True Then
                oRng.Text = "[x]"
            Else
                oRng.Text = "[__]"
            End If
            'Reprotect the document.
            If bProtected = True Then
                ActiveDocument.Protect _
                        Type:=wdAllowOnlyFormFields, _
                        NoReset:=True, _
                        Password:=""
            End If
        End If
    End With
lbl_Exit:
    Exit Sub
End Sub
Public Sub FFOnEntry()
    For Each ofld In ActiveDocument.FormFields
        If ofld.name = mstrFF Then
            bValue = ofld.CheckBox.Value
            'Unprotect the file
            If Not ActiveDocument.ProtectionType = wdNoProtection Then
                bProtected = True
                ActiveDocument.Unprotect Password:=""
            End If
            With ofld
                Set oRng = .Range
                .Range.Delete
                If bValue = True Then
                    oRng.Text = "[x]"
                Else
                    oRng.Text = "[__]"
                End If
            End With
            'Reprotect the document.
            If bProtected = True Then
                ActiveDocument.Protect _
                        Type:=wdAllowOnlyFormFields, _
                        NoReset:=True, _
                        Password:=""
            End If
        End If
        Exit For
    Next ofld
lbl_Exit:
    Exit Sub
End Sub
Private Function GetCurrentFF() As Word.FormField
    Set rngFF = Selection.Range
    rngFF.Expand wdParagraph
    For Each fldFF In rngFF.FormFields
        Set GetCurrentFF = fldFF
        Exit For
    Next fldFF
lbl_Exit:
    Exit Function
End Function
Public Sub SetupFFMacros()
Dim ffItem As Word.FormField
    For Each ffItem In ActiveDocument.FormFields
        ffItem.EntryMacro = "FFOnEntry"
        ffItem.ExitMacro = "FFOnExit"
    Next
lbl_Exit:
    Exit Sub
End Sub
Cabaq
09-10-2015, 09:05 AM
Wow that escalated quickly :)
I'm getting "Object variable or With block variable not set (Error 91)" in the FFOnExit(), any ideas?
Cabaq
09-11-2015, 02:02 AM
Nevermind I solved it. Thanks a lot for your help!! :)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.