B
Bishman
Hi,
I have some issues with the code below.
These are:
ONE: In code I open an existing document and 'attach' the Mail Merge data
source, but the data is not poulating the merge fields until I manually
press 'View Merged Data' button in Word. The data then appears as expected.
If I perform the 'wrdMailMerge.Execute(ref oTrue);' call I get two documents
open. One the 'base' doc wth no merged data. The second one with the merged
data. Basically I want one document open with the mergeed data, without
having to manually hit the button in word. Any ideas please ?
TWO: How can I get the C# app to 'stop' while the Word app is open ?
THREE: After the cleanup calls are made at the bottom of the code I am left
with an 'empty', copy of Word running with no open documents. How can I kill
of the app ?
If all else fails, can someone point me in the direction of a decent Word
Automation / Mail Merge example ?
Any help appreciated.
Thanks
Jon
--------------------------------------------------------------------------------------------------------------------------
try
{
Word.Application wrdApp;
Word._Document wrdDoc;
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
Object oName = sMailMergeInputFile;
Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
Word.MailMergeFields wrdMergeFields;
// Create an instance of Word and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;
// Add a new document.
Object oWordDocument = "C:\\TestLetterMM.doc";
wrdDoc = wrdApp.Documents.Add(ref oWordDocument, ref oMissing,
ref oMissing, ref oMissing);
wrdDoc.Select();
wrdSelection = wrdApp.Selection;
wrdMailMerge = wrdDoc.MailMerge;
// Perform mail merge.
wrdDoc.MailMerge.OpenDataSource(sMailMergeInputFile, 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, ref oMissing);
object oTrue = true;
wrdMailMerge.Execute(ref oTrue);
//wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument;
wrdDoc.Select();
// Close the original form document.
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
// Release References.
wrdSelection = null;
wrdMailMerge = null;
wrdMergeFields = null;
wrdDoc = null;
wrdApp = null;
// Clean up temp file.
System.IO.File.Delete(sMailMergeInputFile);
}
catch (Exception excpt)
{
MessageBox.Show(excpt.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
--------------------------------------------------------------------------------------------------------------------------
I have some issues with the code below.
These are:
ONE: In code I open an existing document and 'attach' the Mail Merge data
source, but the data is not poulating the merge fields until I manually
press 'View Merged Data' button in Word. The data then appears as expected.
If I perform the 'wrdMailMerge.Execute(ref oTrue);' call I get two documents
open. One the 'base' doc wth no merged data. The second one with the merged
data. Basically I want one document open with the mergeed data, without
having to manually hit the button in word. Any ideas please ?
TWO: How can I get the C# app to 'stop' while the Word app is open ?
THREE: After the cleanup calls are made at the bottom of the code I am left
with an 'empty', copy of Word running with no open documents. How can I kill
of the app ?
If all else fails, can someone point me in the direction of a decent Word
Automation / Mail Merge example ?
Any help appreciated.
Thanks
Jon
--------------------------------------------------------------------------------------------------------------------------
try
{
Word.Application wrdApp;
Word._Document wrdDoc;
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
Object oName = sMailMergeInputFile;
Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
Word.MailMergeFields wrdMergeFields;
// Create an instance of Word and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;
// Add a new document.
Object oWordDocument = "C:\\TestLetterMM.doc";
wrdDoc = wrdApp.Documents.Add(ref oWordDocument, ref oMissing,
ref oMissing, ref oMissing);
wrdDoc.Select();
wrdSelection = wrdApp.Selection;
wrdMailMerge = wrdDoc.MailMerge;
// Perform mail merge.
wrdDoc.MailMerge.OpenDataSource(sMailMergeInputFile, 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, ref oMissing);
object oTrue = true;
wrdMailMerge.Execute(ref oTrue);
//wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument;
wrdDoc.Select();
// Close the original form document.
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
// Release References.
wrdSelection = null;
wrdMailMerge = null;
wrdMergeFields = null;
wrdDoc = null;
wrdApp = null;
// Clean up temp file.
System.IO.File.Delete(sMailMergeInputFile);
}
catch (Exception excpt)
{
MessageBox.Show(excpt.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
--------------------------------------------------------------------------------------------------------------------------