Link to home
Start Free TrialLog in
Avatar of Sustainable VBA
Sustainable VBA

asked on

Use Excel VBA to check context control checkboxes in a Word file.

Dear Experts,

Before I go into my question I want to give you information about what I am working with.

Programs and their version
• Word 2013
• Excel 2013
• VBA 7.1

Explanation of the problem
I have a Word document that has both checkboxes and textboxes in it. I am looking for an Excel VBA code that can check a specific checkbox in the Word document based on an excel cell saying “Yes” (checked) or “No” (not checked). Each checkbox thus needs to be linked to a different cell.

Word has 3 types of checkboxes:
• Check box content control
• Check box form field (legacyforms)
• Check box (activeX controls)

I am using the Check Box Content Control. For this checkbox I do not find any clear excel VBA codes online that are used to check these checkboxes in Word. Is there anybody that knows how this can be done. Please also inform me if it only works with the other checkboxes and not with the content controls.

Please be informed that I also placed this question on another forum linked here:
http://www.vbaexpress.com/forum/showthread.php?63148-Use-Excel-VBA-to-check-context-control-checkboxes-in-a-Word-file

Your help is much appreciated. Many thanks in advance .

kind regards,

Sustainable
Avatar of Dorababu M
Dorababu M
Flag of India image

May be you need to loop through the excel and do the following in VBA

doc.FormFields("CheckBox").CheckBox.value=cells[i].Value

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sustainable VBA
Sustainable VBA

ASKER

Dear Dorababu and Graham,

Many thanks for the help. It is much appreciated. I have adjusted Graham his code a little so it fits my needs. It was not working at first but I noticed I had to add the Word library in "Tools" --> "references" --> "Microsoft Word object Library". Now it is working fine.

-----------------
Sub cc()

Dim cc As ContentControl
Dim doc As Document
Dim sdoc As String
Dim oWordapplicatie As Object

        Set oWordapplicatie = CreateObject("Word.Application")

        oWordapplicatie.Visible = True
   
        sdoc = ThisWorkbook.Path & "\templates\" & "Test.docx"
        Set doc = oWordapplicatie.documents.Open(sdoc)
   
        Set cc = doc.SelectContentControlsByTag("MyTag")(1)
        cc.Checked = True

End Sub

-----------------

Do you guys know if it is also possible without using the Word library? Or will you then need one of the other checkbox types?

Kind regards,

Sustainable
Yes. You need to declare the relevant objects as Object, as you already have for the Application, and to  take care of built-in constants. For any constants, you can either use the value directly in the code or you can declare the constant yourself

For the concerned library, you will lose 'Intellisense' and the assistance from the Object Browser(F2). You will not otherwise lose access to objects in the library.

This Word macro code:
Sub ccEarly()
    Dim cc As ContentControl
    Dim doc As Document
    
    Set doc = ActiveDocument
    Set cc = doc.SelectContentControlsByTag("MyTag")(1)
    If cc.Type = wdContentControlCheckBox Then
        cc.Checked = True
    End If
End Sub

Open in new window

could be converted thus
Sub ccLate()
    Dim cc As Object
    Dim doc As Object
    Const wdContentControlCheckBox = 8
    
    Set doc = ActiveDocument
    Set cc = doc.SelectContentControlsByTag("MyTag")(1)
    If cc.Type = wdContentControlCheckBox Then
        cc.Checked = True
    End If
End Sub

Open in new window

Dear Graham,

Thank you for your answer. I see that this code works. I will now try and adjust it for my own situation so it can fill multiple boxes.

Many thanks for your help.

Kind regards,

Sustainable
Good luck