This project has moved. For the latest updates, please go here.

checking a checkbox in a Word DocX file

Sep 13, 2012 at 4:06 AM

how do we find a checkbox having a bookmark and check the control in a docx document?

Feb 26, 2013 at 7:02 PM
I would also know how to do this. I have a number of template documents which contain check boxes. We use a third party tool, but I think we might be able to replace it with DocX... if we can find and check/uncheck the boxes easily.
Mar 11, 2014 at 7:10 PM
I was able to open the docX XML and insert the <checked /> element that sets the checkbox as checked.

To do this I had to:

word xml:
<w:ffData xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:name w:val="IsHavingFun" />
  <w:enabled />
  <w:calcOnExit w:val="0" />
  <w:checkBox>
    <w:sizeAuto />
    <w:default w:val="0" />
    <w:checked />    <<<<--The inserted checked xml element.
  </w:checkBox>
</w:ffData>
c#:
            using (var doc = DocX.Load(templateFilePath))
            {
                var xml = doc.Xml;
                XNamespace ns = xml.Name.NamespaceName;
                var checkBoxName = "IsHavingFun";
                var nodes = xml.Descendants(ns + "ffData"); //This is the parent element for the checkbox.
                nodes.ForEach(x =>
                {//loop thru all checkbox nodes and find the checkbox by name
                   var xAttribute = nameElement.Attribute(ns + "val");
                   if (xAttribute != null && xAttribute.Value == checkBoxName)
                   {//find the checkbox element and insert the new checked element.
                     var checkBox = dataElement.Descendants(ns + "checkBox");
                     var checkedElement = new XElement(ns + "checked");
                     checkBox.ElementAt(0).Add(checkedElement);
                   }
                }
            }
Nov 17, 2014 at 12:23 PM
Edited Nov 17, 2014 at 12:23 PM
I recently tried to do the same but I am seeing a different format for the document (using Word 2010)

First, note that this is a check-box from the developers tab. Once I added it, I clicked the properties button and gave it a tag.

This should results in something like the following:
      <w:sdt>
        <w:sdtPr>
          <w:rPr>
            <w:rFonts w:ascii="Tahoma" w:hAnsi="Tahoma" w:cs="Tahoma" />
            <w:sz w:val="22" />
            <w:szCs w:val="22" />
            <w:rtl />
          </w:rPr>
          <w:alias w:val="visitPurposeNewSite" />
          <w:tag w:val="visitPurposeNewSite" />
          <w:id w:val="567623586" />
          <w14:checkbox>
            <w14:checked w14:val="0" />
            <w14:checkedState w14:val="2612" w14:font="MS Gothic" />
            <w14:uncheckedState w14:val="2610" w14:font="MS Gothic" />
          </w14:checkbox>
        </w:sdtPr>
        <w:sdtContent>
          <w:r w:rsidR="00096598">
            <w:rPr>
              <w:rFonts w:ascii="Segoe UI Symbol" w:eastAsia="MS Gothic" w:hAnsi="Segoe UI Symbol" w:cs="Segoe UI Symbol" w:hint="cs" />
              <w:sz w:val="22" />
              <w:szCs w:val="22" />
              <w:rtl />
            </w:rPr>
            <w:t>☐</w:t>
          </w:r>
        </w:sdtContent>
      </w:sdt>
(Which is quite different from above.
The things to note:
  • Overall the check-box is within sdt element.
  • There are two distinct elements within it - sdtPr which contains the tag, check-box, etc and sdtContent which contains the content (or visual appearance of the check-box)
  • We can use the tag to find the check-box
  • Then we need to manipulate the data (inside checkbox) and the appearance. Note that the default appearance (content) is a non-checked box - <w:t>☐</w:t>.
  • The check-box namespace itself is slightly different than the other namespaces (w14) thus, I am using explicit search on the element local name.
I have create the following code (note, this is within a wrapper. this.theDoc is the Novacode.DocX instance):
        /// <summary>
        /// Set ("check") a checkbox with the specified name.
        /// </summary>
        /// <param name="name">The name of the textbox to check.</param>
        public bool SetCheckBox(string name)
        {
            // get the XML and the namespace
            var xml = this.theDoc.Xml;
            XNamespace ns = xml.Name.NamespaceName;

            var nodes = xml.Descendants(ns + "sdt");  // This is the parent element for the check-box.

            var found = false;
            // loop on all the nodes
            foreach (var cbElementParent in nodes)
            {
                // look for the elements tag with the specified name
                var tagElement = cbElementParent.Descendants(ns + "tag").FirstOrDefault(t => t.Attribute(ns + "val") != null && t.Attribute(ns + "val").Value == name);

                if (tagElement != null)
                {
                    found = true;

                    // get the check-box itself and change it's checked value
                    var cbElement = tagElement.ElementsAfterSelf().FirstOrDefault(e => e.Name.LocalName == "checkbox");

                    if (cbElement != null)
                    {
                        var checkedElement = cbElement.Elements().FirstOrDefault(e => e.Name.LocalName == "checked");

                        checkedElement.Attribute(checkedElement.Name.Namespace + "val").Value = "1";
                    }

                    // change the symbol
                    var contentElement = cbElementParent.Element(ns + "sdtContent");

                    if (contentElement != null)
                    {
                        contentElement.Element(ns + "r").Element(ns + "t").Value = "☒";
                    }
                }              
            }
            return found;
        }
BTW, would be interesting to see how to do it the OO XML SDK and not directly manipulating the XML