C# & Word Mail Merge Problems

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);

}

--------------------------------------------------------------------------------------------------------------------------
 
B

Bishman

Yeah thanks, I have.

It has got me 90% of the way there, Its just the last few areas. The line
'wrdMailMerge.Execute(ref oFalse); is taken from their example, but thats
the one that seems to create a new document within the Word App.

I am not finding it easy to get hold of a decent reference to the Office
object model or any other examples etc.

Jon
 
M

mkoelmans

Killing the app isnt all that diff:
wrdApp.Quit(ref oFalse, ref oMissing ,ref oMissing);


Bishman schreef:
 

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

Top