Vb.net – check a checkbox in word using vb.net

automationms-wordvb.net

I have a word doc with several checkbox fields. I can fill the text fields, but haven't figured out how to check checkboxes.

I can't just make a macro in word and see how word does it because to use the keyboard to check the box (space bar), you have to enable document protection, which disables macro creation.

Best Answer

I just tried setting an old fashion Word2003 Checkbox with VBA. Worked with that piece of code:

' demo purposes - added a command
Private Sub CommandButton1_Click()
    ' FormFields refers to Word2003 FormFields
    If ActiveDocument.FormFields(1).Type = wdFieldFormCheckBox Then
        ActiveDocument.FormFields(1).CheckBox.Value = True
    End If

   ' ContentControls refers to >= Word2007 Controls - thx to StevenDotNet for the hint
   ActiveDocument.ContentControls(1).Checked = True
End Sub

WordCheckBoxControl_Form

On the other hand I created a VS2012 WordProject with VB.net and added some code to check the box on load.

Private Sub ThisDocument_Open() Handles Me.Open
    Me.FormFields(1).CheckBox.Value = True
End Sub
Related Topic