By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

C# & Word Mail Merge Problems

P: n/a
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(sMailMergeInputFil e, 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);

}

--------------------------------------------------------------------------------------------------------------------------
Nov 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Bishman,

Have you gone through http://support.microsoft.com/kb/301659?

Cheers,
Chester

"Bishman" wrote:
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(sMailMergeInputFil e, 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);

}

--------------------------------------------------------------------------------------------------------------------------
Nov 21 '06 #2

P: n/a
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

"Chester" <ch*******@community.nospamwrote in message
news:6E**********************************@microsof t.com...
Hi Bishman,

Have you gone through http://support.microsoft.com/kb/301659?

Cheers,
Chester

"Bishman" wrote:
>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(sMailMergeInputFi le, 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);

}

--------------------------------------------------------------------------------------------------------------------------

Nov 21 '06 #3

P: n/a
Killing the app isnt all that diff:
wrdApp.Quit(ref oFalse, ref oMissing ,ref oMissing);
Bishman schreef:
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

"Chester" <ch*******@community.nospamwrote in message
news:6E**********************************@microsof t.com...
Hi Bishman,

Have you gone through http://support.microsoft.com/kb/301659?

Cheers,
Chester

"Bishman" wrote:
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(sMailMergeInputFil e, 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);

}

--------------------------------------------------------------------------------------------------------------------------
Nov 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.