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: openxmlsdk
This topic contains 2 replies, has 2 voices, and was last updated by koolprasad2003 7 years, 7 months ago.
-
AuthorPosts
-
March 30, 2017 at 7:06 am #4263
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 OpenXMLSDKPlease suggest
Thanks
PrasadMarch 30, 2017 at 2:49 pm #4264Hi 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.
Best, Eric
April 3, 2017 at 5:01 am #4270Hello 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 onlyusing 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 aboveThanks
Prasad -
AuthorPosts
You must be logged in to reply to this topic.