save word after mail merge

G

Guest

Hi,

I'm using the bellow code to mail merge a word document and it's working
fine, where I'm openning "improvement.doc" which is just word document that
contain with some text and the merge fields at the top of the document...
after performing the merge I want to save the merged document as
"merged.doc" and close it. the problem is that my code is opening a new
document (as Letter) to do the merge and I don't know to access that document
to be able to save it and close it!!!?
I have tried to comment the : wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument;
but still doing the same.

Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
Word.MailMergeFields wrdMergeFields;
string StrToAdd;
Object oName = "C:\\improvement.doc";
wrdApp = new Word.Application();
wrdApp.Visible = true;
// Add a new document.
wrdDoc = wrdApp.Documents.Add(ref oName, ref oFalse,
ref oMissing, ref oMissing);
wrdDoc.Select();
wrdSelection = wrdApp.Selection;
wrdMailMerge = wrdDoc.MailMerge;

Object oFileName ="C:\\merged.doc";
Object oFileFormat = Word.WdSaveFormat.wdFormatDocument;
Object oTrue = true;
// Create a MailMerge Data file.
CreateMailMergeDataFile();
wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref oFalse);
wrdDoc.SaveAs(ref oFileName, ref oFileFormat, ref oMissing, ref
oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing);
wrdDoc.Saved = true;
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
//wrdApp.Quit(ref oFalse, ref oMissing, ref oMissing);
// Release References.
wrdSelection = null;
wrdMailMerge = null;
wrdMergeFields = null;
wrdDoc = null;
wrdApp = null;
// Clean up temp file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top