none
Check a checkbox in Word 2010

    Question

  • Hello,

    I searched around how to check a checkbox in an existing word. The way I did it its like this, where checkboxes in the function are the names of the tags I want to check. 

    This code is working, but I had to add these lines

    Text t = block.Parent.Descendants<Text>().FirstOrDefault();
    t.Text = "☒";

    to make it work. The Checked.Val.Value = OnOffValues.True did not make any difference... is that normal? Am i doing something wrong? Is because of the way am I creating the checkbox in the template (did not do anything special I guess)?

    Thanks for the help!

    private static void CheckActivceChecckboxes(MainDocumentPart main, IList<string> checkboxes)
            {
                if (checkboxes == null || checkboxes.Count == 0)
                    return;
                
                var blocks = main.Document.Body.Descendants<SdtRun>().ToList();
                foreach (SdtRun block in blocks)
                {
                    if (block.SdtProperties != null)
                    {
                        Tag tag = block.SdtProperties.ChildElements.OfType<Tag>().ElementAt(0);
                        var name = tag.Val.Value;
                        if (checkboxes.Contains(name))
                        {
                            SdtContentCheckBox chkBox = block.SdtProperties.GetFirstChild<SdtContentCheckBox>();
                            chkBox.Checked.Val.Value = OnOffValues.True;
                            Text t = block.Parent.Descendants<Text>().FirstOrDefault();
                            t.Text = "☒";
                        }
                    }
                }
            }

    Thursday, October 18, 2012 12:50 PM

Answers

  • Hi Diego,

    Thanks for posting in the MSDN Forum.

    Would you please try OnOffValue.One/OnOffValue.Zero? Please try following snippet. It will let checked checkbox became uncheck and let unchecked checkbox became checked.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using W14 = DocumentFormat.OpenXml.Office2010.Word;
    using com.mksword.Net.OpenXmlTools;
    
    namespace ConsoleApplication12
    {
        class CheckCheckBox : OpenXmlUtil
        {
            public void Action()
            {
                OpenFile(OXUFileFilter.WORD);
                string Path = CopyFileName();
                File.Copy(OriginalPath, Path, true);
                using (WordprocessingDocument WPD = WordprocessingDocument
                    .Open(Path, true))
                {
                    MainDocumentPart MDP = WPD.MainDocumentPart;
                    Document D = MDP.Document;
                    foreach (SdtRun SR in D.Descendants<SdtRun>().ToList())
                    {
                        SdtProperties SPS = SR.SdtProperties;
                        W14.SdtContentCheckBox SCCB = SPS
                            .Descendants<W14.SdtContentCheckBox>().FirstOrDefault();
                        SetLog(SCCB.Checked.Val, OXULogType.INFO);
                        if (SCCB.Checked.Val == W14.OnOffValues.Zero)
                        {
                            SCCB.Checked.Val = W14.OnOffValues.One;
                            Text T = SR.Descendants<Text>().FirstOrDefault();
                            T.Text = "☒";
                        }
                        else if (SCCB.Checked.Val == W14.OnOffValues.One)
                        {
                            SCCB.Checked.Val = W14.OnOffValues.Zero;
                            Text T = SR.Descendants<Text>().FirstOrDefault();
                            T.Text = "☐";
                        }
                    }
                    D.Save();
                }
            }
        }
    }

    Hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 5:34 AM
    Moderator

All replies

  • Hi Diego,

    Thanks for posting in the MSDN Forum.

    Would you please try OnOffValue.One/OnOffValue.Zero? Please try following snippet. It will let checked checkbox became uncheck and let unchecked checkbox became checked.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using W14 = DocumentFormat.OpenXml.Office2010.Word;
    using com.mksword.Net.OpenXmlTools;
    
    namespace ConsoleApplication12
    {
        class CheckCheckBox : OpenXmlUtil
        {
            public void Action()
            {
                OpenFile(OXUFileFilter.WORD);
                string Path = CopyFileName();
                File.Copy(OriginalPath, Path, true);
                using (WordprocessingDocument WPD = WordprocessingDocument
                    .Open(Path, true))
                {
                    MainDocumentPart MDP = WPD.MainDocumentPart;
                    Document D = MDP.Document;
                    foreach (SdtRun SR in D.Descendants<SdtRun>().ToList())
                    {
                        SdtProperties SPS = SR.SdtProperties;
                        W14.SdtContentCheckBox SCCB = SPS
                            .Descendants<W14.SdtContentCheckBox>().FirstOrDefault();
                        SetLog(SCCB.Checked.Val, OXULogType.INFO);
                        if (SCCB.Checked.Val == W14.OnOffValues.Zero)
                        {
                            SCCB.Checked.Val = W14.OnOffValues.One;
                            Text T = SR.Descendants<Text>().FirstOrDefault();
                            T.Text = "☒";
                        }
                        else if (SCCB.Checked.Val == W14.OnOffValues.One)
                        {
                            SCCB.Checked.Val = W14.OnOffValues.Zero;
                            Text T = SR.Descendants<Text>().FirstOrDefault();
                            T.Text = "☐";
                        }
                    }
                    D.Save();
                }
            }
        }
    }

    Hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 5:34 AM
    Moderator
  • Hello Tom,

    Thanks for the answer. The code works with the One and Zero but I have to put also the two lines of code

      Text T = SR.Descendants<Text>().FirstOrDefault();
      T.Text = "☐";

    If not the checkbox it is not clear or displayed as checked properly.

    Anyway your code proves for what the Val is used and the conclusion, if correct, it is that changing the val does not change the way the checkbox is displayed. So you have to change both properties separately.

    Thanks again!

    Tuesday, October 23, 2012 10:46 AM