none
Exception when convert .dotx file to docx RRS feed

  • Question

  • Hi,

    I have a word document with extension .dotx, I want to save it as .docx

    I Added method 'convert' before saving the word document to new format. Other wise it is not converted completely to new format but only saves in docx format.

    But for .dotx to .docx, the method 'convert' have an exception: Exception has been thrown by the target of an invocation.

    Could you please give me some help with this?

    Here are the codes:

    public void SaveWordDocTo(object document, string newPath)
            {
                Microsoft.Office.Interop.Word.Document d = (Microsoft.Office.Interop.Word.Document)document;
                object fileName = newPath;
                object fileFormat;
                object missing = Type.Missing;
                object falseObj = false;

                if (System.IO.Path.GetExtension(newPath).EndsWith("docx"))
                {
                   
                    d.GetType().InvokeMember("Convert", BindingFlags.InvokeMethod, null, d, null); --- this will cause an exception

                    fileFormat = (WdSaveFormat)16; //12 - wdFormatXMLDocument (XML document format.)
                }
               
                d.SaveAs(ref fileName, ref fileFormat, ref missing, ref missing, ref falseObj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }

    Thanks very much,

    Wendy

    Friday, July 27, 2012 5:31 AM

Answers

  • Hi Wendy

    I'm not familiar with the Convert method, although I see it was added in version 2007. I have no idea how it's supposed to be used and the object model Help doesn't provide any useful information at all...

    Historically, it has not been possible to change a *.dotx to a *.docx directly. It's been necessary to use Documents.Add to generate a new document from a *.dotx. This will then save with no problems to a *.docx (and that would be the installation default when using SaveAs).

    In more recent versions of Word (with the introduction of the new file formats in Word 2007, I believe) it's possible to save a *.dotx directly to *.docx using SAveAs. The key is that you need to specify the file format, then it should work - without using Convert.


    Cindy Meister, VSTO/Word MVP

    Friday, July 27, 2012 8:15 AM
    Moderator
  • Thank you so much for your help.
    • Marked as answer by WendyHuo Thursday, August 23, 2012 1:14 AM
    Friday, July 27, 2012 8:35 AM

All replies

  • Hi Wendy

    I'm not familiar with the Convert method, although I see it was added in version 2007. I have no idea how it's supposed to be used and the object model Help doesn't provide any useful information at all...

    Historically, it has not been possible to change a *.dotx to a *.docx directly. It's been necessary to use Documents.Add to generate a new document from a *.dotx. This will then save with no problems to a *.docx (and that would be the installation default when using SaveAs).

    In more recent versions of Word (with the introduction of the new file formats in Word 2007, I believe) it's possible to save a *.dotx directly to *.docx using SAveAs. The key is that you need to specify the file format, then it should work - without using Convert.


    Cindy Meister, VSTO/Word MVP

    Friday, July 27, 2012 8:15 AM
    Moderator
  • Thank you so much for your help.
    • Marked as answer by WendyHuo Thursday, August 23, 2012 1:14 AM
    Friday, July 27, 2012 8:35 AM