How to read Form fields (ActiveX contol) values from word 2010 using OpenxmlSDK

Home Forums Open-Xml-Sdk How to read Form fields (ActiveX contol) values from word 2010 using OpenxmlSDK

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by  koolprasad2003 7 years ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #4263

    koolprasad2003
    Participant

    Dear team
    I have a word 2010 dopcument, in which I have placed some Activex component in it, now I want to read its values
    how to do it using OpenXMLSDK

    Please suggest
    Thanks
    Prasad

    #4264

    Eric White
    Keymaster

    Hi Prasad,

    I’m not sure how the ActiveX controls store the data, but an easy way to find out is to use the Open-Xml-Sdk productivity tool to compare before/after versions of the document after you change values.

    How to Research Open XML Markup

    Best, Eric

    #4270

    koolprasad2003
    Participant

    Hello Eric
    Thanks for the reply.
    Sure. The easiest way to find out is to compare before and after version of document using Productivity tool.
    Actually following code will work to fetch values of ActiveX control but for word 2007 only

    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using System.Xml;
    using System.IO;
    using System.Text;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml.Packaging;
    
    namespace OpenXMLTest
    {
        class Program
        {
            const string textBoxId = "{8BD21D10-EC42-11CE-9E0D-00AA006002F3}";<
    span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">
            const string radioButtonId = "{8BD21D50-EC42-11CE-9E0D-00AA006002F3}";
            const string checkBoxId = "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}";
    
            static void Main(string[] args)
            {
                string fileName = @"C:\Users\Andy\Desktop\test_l1demo.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false))
                {
                    foreach (Control control in doc.MainDocumentPart.Document.Body.Descendants())
                    {
                        Console.WriteLine();
                        Console.WriteLine("Control {0}:", control.Name);
                        Console.WriteLine("Id: {0}", control.Id);
    
                        displayControlDetails(doc, control.Id);
                    }
                }
    
                Console.Read();
            }
    
            private static void displayControlDetails(WordprocessingDocument<
    span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);"> doc, StringValue controlId)
            {
                string classId, type, value;
    
                OpenXmlPart part = doc.MainDocumentPart.GetPartById(controlId);
                OpenXmlReader reader = OpenXmlReader.Create(part.GetStream());
                reader.Read();
                OpenXmlElement controlDetails = reader.LoadCurrentElement();
    
                classId = controlDetails.GetAttribute("classid", controlDetails
    .NamespaceUri).Value;
    
                switch (classId)
                {
                    case textBoxId:
                        type = "TextBox";
                        break;
                    case radioButtonId:
                        type = "Radio Button";
                        break;
                    case checkBoxId:
                        type = "CheckBox";
                        break;
                    default:
                        type = "Not known";
                        break;
                }
    
                value = "No value attribute"; //displays this if there is no "value" attribute found
                foreach (OpenXmlElement child in controlDetails.Elements())
                {
                    if (child.GetAttribute("name", controlDetails.NamespaceUri).Value == "Value")
                    {
                        //we've found the value typed by the user in this control
                        value = child.GetAttribute("value", controlDetails.NamespaceUri).Value;
                    }
                }
    
                reader.Close();
    
                Console.WriteLine("Class id: {0}", classId);
                Console.WriteLine("Control type: {0}", type);
                Console.WriteLine("Control value: {0}", value);
    
            }
        }
    }
    
    

    I observed, the way word 2007 store ActiveX values are different than word 1010, I think tags are changed.
    and the code is somehow not working for word 2010 and above

    Thanks
    Prasad

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.