Kaouthar Ell Az February 2016

Create a Ms Word template based on another template using c#

I tried to create a word template based on another Exist template, I have already tried with .docx using bookmarks and it is work but i have no idea how can i change the extension from .docx to .dotx

Here is the code I've tried

        object outputf = document.FullName.Replace(".docx", ".dotx");
        object fileFormat = Word.WdSaveFormat.wdFormatTemplate;
        document.SaveAs(ref outputf, ref fileFormat, ref oMissing,
    ref oMissing,  ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing);

But it does not result in a .dotx that Word can work with. Any ideas?

Answers


Cindy Meister February 2016

The only way to turn a Word document (docx) into a template (dotx) is to use the SaveAs command, specifying not only the file name parameter but also the FILE TYPE. This takes a Enum, as you show in your comment.

The trick is that the Enum MUST fit the file extension. I believe wdFormatTemplate is the old .dot file format. Look for an Enum like WdFormatXMLTemplate. (I'd look it up for you, but MSDN isn't letting me log in to the Language Reference...)

Note: It's always important that you describe HOW something "doesn't work". For example, any error messages.

Post Status

Asked in February 2016
Viewed 2,469 times
Voted 5
Answered 1 times

Search




Leave an answer