mgalal
mgalal

Reputation: 437

Open word template and save as

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.

Upvotes: 0

Views: 4588

Answers (1)

Bill Tarbell
Bill Tarbell

Reputation: 5234

object paramMissing = Type.Missing;
object fileFormat = wdSaveFormat.[whatever you want]
object filenameOut = @"c:\somefile.extension";

doc.SaveAs(ref filenameOut, ref fileFormat,
              ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
              ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, 
              ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, 
              ref paramMissing, ref paramMissing);

Some info and examples here:

http://msdn.microsoft.com/en-us/library/bb412305.aspx

Upvotes: 1

Related Questions