none
Trying to change a ContentControls CheckBox state in C# using Interop.Word RRS feed

  • Question

  • Hi :)

    I'm working in C# and as the thread title states, I'm trying to change the state of a CheckBox Content Control to true.

    I have a Word docx document template that has the Checkbox in a Bookmark range.  

    I'm able to change text in a bookmark with following function :

    private void ChangeBookmarkText(Microsoft.Office.Interop.Word.Document doc, string BookmarkName, string Info)
            {
                if (doc.Bookmarks.Exists(BookmarkName))
                {
                    Object name = BookmarkName;
                    Microsoft.Office.Interop.Word.Range range = doc.Bookmarks.get_Item(ref name).Range;
                    
                    range.Text = Infos;
                    object newRange = range;
                    doc.Bookmarks.Add(BookmarkName, ref newRange);
                }
            }

    But when it comes to changing a CheckBox, I'm totally helpless.  I know I probably have to take into consideration the range of the bookmark, but which property do I have to use in order to change the .Value to true...

    Any help as how to deal with that issue would be appreciated. 

    Thank you for your time !

    Edit*  I've got VS2012 and Word2010


    • Edited by Luthecia Friday, October 18, 2013 10:14 PM Added dev versions
    Friday, October 18, 2013 10:12 PM

Answers

  • Hello Again :)


    :I cheated!  I finally decided to use the Range.InsertSymbol Method instead

    private void FakeCheckBoxes(Microsoft.Office.Interop.Word.Document doc, string Bookmark)
            {
                if (doc.Bookmarks.Exists(Bookmark))
                {
                    Object name = Bookmark;
                    Microsoft.Office.Interop.Word.Range 
    range = doc.Bookmarks.get_Item(ref name).Range;
    
                    object Font = "Wingdings";
                    object Unicode = Type.Missing;
                    object Bias = Type.Missing;
                    range.InsertSymbol(253, ref Font, ref Unicode, ref Bias);
                }
    I have a box, that looks like a CheckBox and since I'm just filling a  Word Template, it works wonders.
    • Marked as answer by Luthecia Saturday, October 19, 2013 12:19 AM
    Saturday, October 19, 2013 12:19 AM

All replies

  • Hello Again :)


    :I cheated!  I finally decided to use the Range.InsertSymbol Method instead

    private void FakeCheckBoxes(Microsoft.Office.Interop.Word.Document doc, string Bookmark)
            {
                if (doc.Bookmarks.Exists(Bookmark))
                {
                    Object name = Bookmark;
                    Microsoft.Office.Interop.Word.Range 
    range = doc.Bookmarks.get_Item(ref name).Range;
    
                    object Font = "Wingdings";
                    object Unicode = Type.Missing;
                    object Bias = Type.Missing;
                    range.InsertSymbol(253, ref Font, ref Unicode, ref Bias);
                }
    I have a box, that looks like a CheckBox and since I'm just filling a  Word Template, it works wonders.
    • Marked as answer by Luthecia Saturday, October 19, 2013 12:19 AM
    Saturday, October 19, 2013 12:19 AM
  • Hi Luthecia

    I'm glad you found a solution that works for you. For the sake of completeness, for others searching for the same information who come across this thread, here's some VBA code that picks up the first content control with the specified Title and "toggles" the Checked property.

        Dim sControlName As String
        Dim ccs As Word.Contentcontrols
        
        sControlName = "Check1"
        Set ccs = ActiveDocument.SelectContentControlsByTitle(sControlName)
        ccs(1).Checked = Not ccs(1).Checked
    

    For VB.NET, drop the "Set" in the fourth line of code.

    Converting to C# shouldn't be a problem as long as one remembers to use [1] to specify the member of the collection.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, October 19, 2013 7:04 PM
    Moderator