none
How to read ActiveX Checkbox Value using C# Word automation? RRS feed

  • Question

  • I am using C# word automation to read through a form created using Microsoft Word.  The form contains ActiveX checkbox controls.  I am a bit clueless on how to check these fields and have searched high and low for sample code but have come up empty.

    Could someone point me in the right direction?  Say I have an ActiveX checkbox (called "Check1") in a word form, what kind of code do I need to test if it is checked or not?

    Thanks in advance,

    Scott Lee

    Thursday, May 8, 2014 9:38 PM

Answers

  • Hi Scott,

    What’s version of .Net framework the project target? If you are using .Net framework 3.5 or previous, you need to use the Type.Invoke as Cindy metioned because these version of .Net framework don’t support dynamic language. Here is a sample to use Type.Invoke to get the property of an object for your reference:

    public static void Main()
    
            {
    
                Word.Application wordApp = new Word.Application();
    
                Word.Document doc = wordApp.Documents.Open(@"C:\Users\UserName\Desktop\ReadActiveControl.docx");
    
                foreach (InlineShape shape in doc.InlineShapes)
    
                {
    
                    string name = shape.OLEFormat.Object.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null).ToString();
    
                    string value = shape.OLEFormat.Object.GetType().InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null).ToString();
    
                    if (shape.OLEFormat != null && shape.OLEFormat.ClassType == "Forms.CheckBox.1" && name == "CheckBox1")
    
                    {
                        Console.WriteLine("{0}'s value is {1}", name, value);
    
                    }
    
                }
    
                doc.Close();
    
                wordApp.Quit();
    
            }
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Scotty Lee Wednesday, May 21, 2014 3:57 PM
    Wednesday, May 21, 2014 9:22 AM
    Moderator

All replies

  • Based on the description, you want to read the value from activeX checkbox. As far as I know, the checkbox is an InlineShape, and we can get the value via InlineShape. OLEFormat.Object.Value.

    Here is a sample for your reference:
     

     public void Main()
    
            {
    
                Word.Application wordApp = new Word.Application();
    
                Word.Document doc= wordApp.Documents.Open(@"C:\Users\v-fexue\Desktop\ReadActiveControl.docx");
    
                foreach(InlineShape shape in doc.InlineShapes)
    
                {
    
                    if (shape.OLEFormat != null && shape.OLEFormat.ClassType == "Forms.CheckBox.1" && shape.OLEFormat.Object.Name == "CheckBox1")
    
                    {
    
                        Console.WriteLine("{0}'s value is {1}", shape.OLEFormat.Object.Name,shape.OLEFormat.Object.Value);
    
                    }
    
                }
    
                doc.Close();
    
                wordApp.Quit();
    
            }
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 9, 2014 10:03 AM
    Moderator
  • Fei,

    Thanks for the quick reply.  I tried your code and I think I'm missing a reference somewhere, since the compiler doesn't recognize "Name" or "Value" for the Object in this line:

    **************

        Console.WriteLine("{0}'s value is {1}", shape.OLEFormat.Object.Name,shape.OLEFormat.Object.Value);

    Error 2 'object' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 

    Error 3 'object' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

    **************

     

    So what else do I need to include in order for this work?  I must admit that I am a bit of a novice in visual studio.  I am currently using visual studio 2008.  My using statements are:

    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Word;
    using WordApp = Microsoft.Office.Interop.Word;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Collections;
    using System.Diagnostics;

    Thanks again!

    Friday, May 9, 2014 7:20 PM
  • Hi Scotty

    Fei's code appears to assume you're using a version of C# that can work with "late binding". If you're not, then you need to use PInvoke (Type.InvokeMember) in order to run calls on untyped Office objects. See for example

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/bb562237-09c3-437e-9e80-6666cf215ddc/accessing-shapeoleformatobjectname-attribute-from-c?forum=vsto
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/6297712c-6892-40b8-8387-f0b784352bd4/how-to-populate-word-2003-combobox-dropdown-list-using-vsto-c?forum=vsto


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, May 11, 2014 3:53 PM
    Moderator
  • Cindy/Fei,

    Thanks for the reply.  I am using Visualstudio 2008, so I assume that my computer does not have "late binding".   I'm having trouble trying to figure out how to use InvokeMember to get to these values from the examples you listed (I'm a novice programmer at best and truthfully I didn't understand most of it).  I would appreciate it if you or anyone could provide me a code snipet of how to access these values from Fei's example:

    shape.OLEFormat.Object.Name

    shape.OLEFormat.Object.Value

    Thanks again,

    Scott.

    Tuesday, May 20, 2014 5:51 PM
  • Hi Scott,

    What’s version of .Net framework the project target? If you are using .Net framework 3.5 or previous, you need to use the Type.Invoke as Cindy metioned because these version of .Net framework don’t support dynamic language. Here is a sample to use Type.Invoke to get the property of an object for your reference:

    public static void Main()
    
            {
    
                Word.Application wordApp = new Word.Application();
    
                Word.Document doc = wordApp.Documents.Open(@"C:\Users\UserName\Desktop\ReadActiveControl.docx");
    
                foreach (InlineShape shape in doc.InlineShapes)
    
                {
    
                    string name = shape.OLEFormat.Object.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null).ToString();
    
                    string value = shape.OLEFormat.Object.GetType().InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null).ToString();
    
                    if (shape.OLEFormat != null && shape.OLEFormat.ClassType == "Forms.CheckBox.1" && name == "CheckBox1")
    
                    {
                        Console.WriteLine("{0}'s value is {1}", name, value);
    
                    }
    
                }
    
                doc.Close();
    
                wordApp.Quit();
    
            }
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Scotty Lee Wednesday, May 21, 2014 3:57 PM
    Wednesday, May 21, 2014 9:22 AM
    Moderator
  • Thank you Fei, this is exactly what I needed.   I am indeed using .NET 3.5.

    Wednesday, May 21, 2014 3:58 PM
  • private void button1_Click(object sender, EventArgs e)
            {
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                Word.Document doc = wordApp.Documents.Open(@"C:\1.docx");
                foreach (Field f in doc.Fields)
                {
                    if (f.Type == WdFieldType.wdFieldOCX)
                    {
                            if (f.OLEFormat != null)
                            {
                                if (f.OLEFormat.ProgID.ToLowerInvariant().Contains("checkbox"))
                                {
                                    string t = f.OLEFormat.Object.Name.ToString();
                                    if (t == "CheckBox1")
                                    {
                                        f.OLEFormat.Object.Value = true;
                                    }
                                }
                            }
                    }
                }
                doc.Close();
                wordApp.Quit();
            }


    Wednesday, July 16, 2014 7:04 AM