Share via


C# open word template and saveas

Question

Friday, October 12, 2012 5:56 PM | 1 vote

I'm using C# to read data from a CSV file and update the values of bookmarks in a word document. I have it working, however, I want it to open the template file, update it and save it under another name.

I used this code:

_Application word = new Application();
Document doc = word.Documents.Open(@"D:\Documents\Bookmarked.dot");
doc.Bookmarks["mybookmark"].Select();
word.Selection.TypeText("Replacement text");
((_Application)word).Quit(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat);

From here: http://social.msdn.microsoft.com/Forums/en-HK/csharpgeneral/thread/32b25cfd-cc5b-4e9f-bcbf-0dbbd49bca02

I just don't know how to save it under another name.

Thanks,

All replies (3)

Tuesday, October 16, 2012 8:35 AM ✅Answered | 1 vote

Hi Mg527,

Thank you for posting in the MSDN Forum.

If you want to create a Word Document based on a template, you can use Documents.Add() method.

In your code, it could be like this

Document doc = word.Documents.Add(@"D:\Documents\Bookmarked.dot");

Later when you want to save the document in a specific directory with a specific name, you can use Document.SaveAs2() method.

In your code, it could be like this

doc.SaveAs2(@"E:\Documents\Data.docx"); //Detailed usage of each parameter can be seen on the reference page.

After the document been saved, there's no need to pass WdSaveOptions.wdSaveChanges to word.Quit() method. You can simply end your application with

doc.Close(); //This is used to close document.
word.Quit(); //This is used to quit the Word application.
GC.Collect(); //This is used for Garbage collection.

For more infomation about Word Object Model, you can refer to this page.

Hope it helps.

Best regards,

Quist Zhang [MSFT]
MSDN Community Support | Feedback to us


Friday, October 12, 2012 9:29 PM

Check below code using field tags like <tag>   (all tags will replace with the data from database or other source). Bookmark may be another strategy to get this done.

using Word = Microsoft.Office.Interop.Word;

void CreateWordDocument(object fileName, object NewFileName)
{
 
 object missing = System.Reflection.Missing.Value;
 Word.Application wordApp = new Word.ApplicationClass();
 Word.Document aDoc = null;
 aDoc = wordApp.Documents.Open(ref fileName, ref missing,
                    ref readOnly, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref isVisible, ref missing, ref missing,
                    ref missing, ref missing);

 aDoc.Activate();

 this.FindAndReplace(wordApp, "<Data1>", Data1Value);
 this.FindAndReplace(wordApp, "<data2>", Data2Value);

 aDoc.SaveAs(ref NewFileName, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
       
 aDoc.Close(ref missing, ref missing, ref missing);
 wordApp.Quit(Type.Missing, Type.Missing, Type.Missing)

}
private void FindAndReplace(Word.Application WordApp,
                                    object findText,
                                    object replaceWithText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            WordApp.Selection.Find.Execute(ref findText,
                ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike,
                ref nmatchAllWordForms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replace, ref matchKashida,
                ref matchDiacritics, ref matchAlefHamza,
                ref matchControl);
        }


Tuesday, October 16, 2012 8:04 AM

Hi mg527,

From your description , I ‘d like to move this post to  the most related forum .

There has more  experts in this aspect  , so you will get  better support and  may have more luck getting answers .

Thanks for your understanding .

Regards ,

Lisa Zhu [MSFT]
MSDN Community Support | Feedback to us