tags:

views:

26

answers:

1

This one very simple thing I can't find the right technique. What I want is to open a .dotx template, make some changes and save as the same name but .docx extension. I can save a WordprocessingDocument but only to the place it's loaded from. I've tried manually constructing a new document using the WordprocessingDocument with changes made but nothing's worked so far, I tried MainDocumentPart.Document.WriteTo(XmlWriter.Create(targetPath)); and just got an empty file.

What's the right way here? Is a .dotx file special at all or just another document as far as the SDK is concerned - should i simply copy the template to the destination and then open that and make changes, and save? I did have some concerns if my app is called from two clients at once, if it can open the same .dotx file twice... in this case creating a copy would be sensible anyway... but for my own curiosity I still want to know how to do "Save As".

+1  A: 

I would suggest just using File.IO to copy the dotx file to a docx file and make your changes there, if that works for your situation. There's also a ChangeDocumentType function you'll have to call to prevent an error in the new docx file.

            File.Copy(@"\path\to\template.dotx", @"\path\to\template.docx");

            using(WordprocessingDocument newdoc = WordprocessingDocument.Open(@"\path\to\template.docx", true))
            {
                newdoc.ChangeDocumentType(WordprocessingDocumentType.Document);
                //manipulate document....
            }
M_R_H