none
What is the proper way to use the ActiveXControlData class? RRS feed

  • Question

  • The ActiveXControlData class constructor argument can be either of type OpenXmlElement or of type String. When I initialize a new instance with an OpenXmlElement from an EmbeddedControlPersistencePart it doesn’t throw an exception but its properties are not set properly. If instead I initialize a new instance with the OuterXml of the OpenXmlElement its properties are set properly.

    Here is the related code snippet: (ControlData is not initialized properly)

    Public Function GetOpenXmlElement(Part As EmbeddedControlPersistencePart) As OpenXmlElement
        Dim Reader As OpenXmlReader = OpenXmlReader.Create(Part.GetStream)
        Reader.Read()
        Dim Element As OpenXmlElement = Reader.LoadCurrentElement
        Return Element
    End Function
    
    Part = XmlDocument.MainDocumentPart.EmbeddedControlPersistenceParts(0)
    Element = GetOpenXmlElement(Part)
    ControlData = New ActiveXControlData(Element)

    But this way ControlData is initialized properly:

    ControlData = New ActiveXControlData(Element.OuterXml)

    How can I initialize ActiveXControlData properly from an OpenXmlElement argument?

    Thanks!






    • Edited by VAlain Tuesday, December 29, 2015 8:55 PM
    Tuesday, December 29, 2015 8:52 PM

All replies

  • >>>How can I initialize ActiveXControlData properly from an OpenXmlElement argument?

    ActiveXControlData constructor (OpenXmlElement[]) that initializes a new instance of the ActiveXControlData class with the specified child elements.

    According to your description, I have made a sample to reproduce your issue, but I am not able find any solution to overcome it. This is a workaround that you could to refer to:

    using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(@"D:\EmbeddedControl.docx", true))
    {
                    
        var Part = doc.MainDocumentPart.EmbeddedControlPersistenceParts.First();
                    
        OpenXmlReader reader = OpenXmlReader.Create(Part);
        reader.Read();
        OpenXmlElement element = reader.LoadCurrentElement();
    
        ActiveXControlData controlData = new ActiveXControlData(element);
        Console.WriteLine(controlData.ChildElements.First().OuterXml);
        OpenXmlElement chdElement = controlData.ChildElements.First();
        Console.WriteLine(chdElement.GetAttribute("classid",chdElement.NamespaceUri).Value);
        Console.ReadLine();
    }
    

    Wednesday, December 30, 2015 6:49 AM
  • Your code make the same as mine and doesn't initialize ControlData properly, the property ActiveXControlClassId should be set as it is when initializing with the OuterXml of Element but the property is empty.

    The problem is because the <ax:ocx> node is put inside an other <ax:ocx> node instead of at the root level as it should be.

    • Edited by VAlain Wednesday, December 30, 2015 4:13 PM
    Wednesday, December 30, 2015 7:49 AM
  • >>>The problem is because the <ax:ocx> node is put inside an other <ax:ocx> node instead of at the root level as it should be.

    According to your description, I have made a sample to remove the <ax:ocx> node is put inside an other <ax:ocx> node instead of at the root level, but I am still unable to get the property ActiveXControlClassId, you could refer to below code:

    using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(@"D:\EmbeddedControl.docx", true))
    {
    
          foreach (Control control in doc.MainDocumentPart.Document.Body.Descendants<Control>())
          {
                  Console.WriteLine();
                  Console.WriteLine("Control {0}:", control.Name);
                  Console.WriteLine("Id: {0}", control.Id);
    
                  OpenXmlPart part = doc.MainDocumentPart.GetPartById(control.Id);
                  OpenXmlReader reader = OpenXmlReader.Create(part.GetStream());
                  reader.Read();
                  OpenXmlElement element = reader.LoadCurrentElement();
                  Console.WriteLine(element.OuterXml);
    
                  ActiveXControlData controlData = new ActiveXControlData(element);
                  Console.WriteLine(controlData.Id);
         }
    
    }

    Note: "<ax:ocx xmlns:ax="http://schemas.microsoft.com/office/2006/activeX" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" ax:classid="{8BD21D10-EC42-11CE-9E0D-00AA006002F3}" ax:persistence="persistStorage" r:id="rId1" />"


    Friday, January 1, 2016 8:29 AM
  • This snippet show how to initialize ActiveXControlData properly with a string (OuterXml):

    using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(@"D:\EmbeddedControl.docx", true))
    {
    
          foreach (Control control in doc.MainDocumentPart.Document.Body.Descendants<Control>())
          {
                  Console.WriteLine();
                  Console.WriteLine("Control {0}:", control.Name);
                  Console.WriteLine("Id: {0}", control.Id);
    
                  OpenXmlPart part = doc.MainDocumentPart.GetPartById(control.Id);
                  OpenXmlReader reader = OpenXmlReader.Create(part.GetStream());
                  reader.Read();
                  OpenXmlElement element = reader.LoadCurrentElement();
                  Console.WriteLine(element.OuterXml);
    
                  ActiveXControlData controlData = new ActiveXControlData(element.OuterXml);
                  Console.WriteLine(controlData.ActiveXControlClassId);
         }
    
    }

    This snippet show how the <ax:ocx> get inside an other <ax:ocx> when ActiveXControlData is initialized with an OpenXmlElement:

    using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(@"D:\EmbeddedControl.docx", true))
    {
    
          foreach (Control control in doc.MainDocumentPart.Document.Body.Descendants<Control>())
          {
                  Console.WriteLine();
                  Console.WriteLine("Control {0}:", control.Name);
                  Console.WriteLine("Id: {0}", control.Id);
    
                  OpenXmlPart part = doc.MainDocumentPart.GetPartById(control.Id);
                  OpenXmlReader reader = OpenXmlReader.Create(part.GetStream());
                  reader.Read();
                  OpenXmlElement element = reader.LoadCurrentElement();
                  Console.WriteLine(element.OuterXml);
    
                  ActiveXControlData controlData = new ActiveXControlData(element);
                  Console.WriteLine(controlData.ActiveXControlClassId);
                  Console.WriteLine(controlData.OuterXml);
         }
    
    }

    • Edited by VAlain Friday, January 1, 2016 4:12 PM
    Friday, January 1, 2016 4:12 PM
  • We are doing the research about your problem. There might be some delay about the response. Appreciate your patience.
    Monday, January 4, 2016 9:56 AM