none
Determine the value of a checkbox on a document page. RRS feed

  • Question

  • I have a simple checkbox on the first page of a document and I would like to use a macro to check it's value after selecting a button to run a macro.

    I can find the value of the document:

    Debug.Print ActiveDocument.Name

    I can't find the checkbox (neither of these work - both return "empty"):

        Debug.Print ActiveDocument.FormFields("CheckBox1").Name
        Debug.Print ActiveDocument.Shapes(1).Name

    Ideas on how to address the checkbox and then return it's value?


    Bob
    Tuesday, October 11, 2011 9:21 PM

Answers

  • Based on your code, I have a content control and not a form field.

    I modified your code a little to find a particular checkbox "tag" and then find it's value.  In this case, I named my Checkbox "CheckBox1Tag".  I am using Office 2010.  The code below does the trick.  Thanks for the help.

     

     

    Sub FormFieldsName()
        Dim doc As Word.Document
        Dim ctl As Word.ContentControl
        Set doc = Word.ActiveDocument
        
        For Each ctl In doc.ContentControls
            Debug.Print "Control Type: " & ctl.Type
            Debug.Print ctl.Tag
      
            If ctl.Tag = "CheckBox1Tag" Then
                Checkboxvalue = ctl.Checked
                Debug.Print "Checkbox1 Value: " & ctl.Checked
            End If
        
        Next
    End Sub

     


    Bob
    • Edited by Boblam Wednesday, October 12, 2011 3:41 AM Update code
    • Marked as answer by Boblam Wednesday, October 12, 2011 3:41 AM
    Wednesday, October 12, 2011 3:38 AM

All replies

  • You didn't mention what version of Word but the check box is probably a Content Control and a check box type would be 8.

    Try this macro and see if it helps:

    Sub FormFieldsName()
        Dim doc As Word.Document
        Dim fld As Word.FormField
        Dim ctl As Word.ContentControl
        Set doc = Word.ActiveDocument
        
        For Each fld In doc.FormFields
            Debug.Print fld.Name
        Next
        For Each ctl In doc.ContentControls
            Debug.Print ctl.Type
        Next
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Wednesday, October 12, 2011 2:34 AM
  • Based on your code, I have a content control and not a form field.

    I modified your code a little to find a particular checkbox "tag" and then find it's value.  In this case, I named my Checkbox "CheckBox1Tag".  I am using Office 2010.  The code below does the trick.  Thanks for the help.

     

     

    Sub FormFieldsName()
        Dim doc As Word.Document
        Dim ctl As Word.ContentControl
        Set doc = Word.ActiveDocument
        
        For Each ctl In doc.ContentControls
            Debug.Print "Control Type: " & ctl.Type
            Debug.Print ctl.Tag
      
            If ctl.Tag = "CheckBox1Tag" Then
                Checkboxvalue = ctl.Checked
                Debug.Print "Checkbox1 Value: " & ctl.Checked
            End If
        
        Next
    End Sub

     


    Bob
    • Edited by Boblam Wednesday, October 12, 2011 3:41 AM Update code
    • Marked as answer by Boblam Wednesday, October 12, 2011 3:41 AM
    Wednesday, October 12, 2011 3:38 AM