none
Converting the current MS Word document to xml and saving it in a target file RRS feed

  • Question

  • Hi All,

    In my add -in I want to build a feature which converts the currently opened document to xml and saves this xml in a separate file.

    But i want this conversion to happen with a specific DTD file.I 'am able to convert , but not according to the standards,also this conversion seems to be having errors.Any help is much much appreciated.This is what I tried so far:

                        

    private void button_convert(object sender, RibbonControlEventArgs e)
            {

               Word._Document oDoc;
                oDoc = Globals.ThisAddIn.Application.ActiveDocument;
                //Creating the instance of Word Application
             object Target = @"c:\\abc\\Target.XML";
                 object Unknown = Type.Missing;
               // Specifying the format in which you want the output file 
                 object format = Word.WdSaveFormat.wdFormatXMLDocument;

                //Changing the format of the document
                oDoc.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown);

            }


    Thursday, April 16, 2015 5:43 AM

Answers

  • Hi Apoorva Sridharan,

    As far as I know, there are two XML format we can convert the document. The first is Word XML Document and second is Word 2003 XML Document. The Word object model doesn't support to convert the document based on the custom DTD.

    As a workaround, we may consider to convert the exits document with specific DTD.

    Hope it is helpful.

    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. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, April 17, 2015 2:07 AM
    Moderator
  • Hi  Apoorva Sridharan,

    This forum is for developers discusing developing issue about VSTO solution, since the issue is more relative to XML developing, I would suggest that you try to reopen a new thread in XML, System.Xml, MSXML and XmlLite forum.

    Hope it is helpful.

    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.

    Monday, April 20, 2015 5:27 AM
    Moderator

All replies

  • Hi Apoorva Sridharan,

    As far as I know, there are two XML format we can convert the document. The first is Word XML Document and second is Word 2003 XML Document. The Word object model doesn't support to convert the document based on the custom DTD.

    As a workaround, we may consider to convert the exits document with specific DTD.

    Hope it is helpful.

    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. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, April 17, 2015 2:07 AM
    Moderator
  • Thanks a ton Fei ,just what I needed. But could you please elaborate on the workaround you provided, I'm not very clear with the steps to take.
    Friday, April 17, 2015 3:49 AM
  • Hi  Apoorva Sridharan,

    This forum is for developers discusing developing issue about VSTO solution, since the issue is more relative to XML developing, I would suggest that you try to reopen a new thread in XML, System.Xml, MSXML and XmlLite forum.

    Hope it is helpful.

    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.

    Monday, April 20, 2015 5:27 AM
    Moderator
  • I used following simple method to convert Word to XML in C#.

                //Create word document
                Document document = new Document();
                document.LoadFromFile(@"D:\Sample.doc");
    
                //Save doc file.
                document.SaveToFile("Sample.xml", FileFormat.Xml);

    Tuesday, May 5, 2015 8:31 AM
  • Hi Apoorva

    Word (and Office) don't support DTD only XLST (Transforms). It's possible to apply an XSLT when opening a Word document, so one possible approach could be to convert the DTD to a transform, open the document applying the transform, then save the result.

    Another possibility would be to access the document's Open XML and apply the DTD to that, programmatically. However, a Word file is actually saved as multiple XML files within a ZIP package so feasibility is a question of what kind of content you want to "transform"?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 20, 2015 6:45 PM
    Moderator