473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# & Word Mail Merge Problems

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
3 11997
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Julia Baresch | last post by:
Everyone, I posted the message below back in February and didn't get any information. I also wasn't able to find any documentation in MS Help, web site, or other groups. I'm posting now with...
1
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word...
10
by: John | last post by:
Hi I am trying to do a word mailmerge form within my vb.net app. My problem is how to do a query on one of my tables and use the result as the mail merge datasource. Any help would be...
1
by: John Welch | last post by:
I'm trying to use the Mergit() function from the MS Knowledge base article to merge data from a query or table in my Access 2000 mdb into an existing word merge document. Every time it runs it...
2
by: Colin Halliday | last post by:
I have a Word 2003 mail merge main document (form letter) that is linked to another Word document data source for the mail merge. If I open this doc using the Word GUI, it first asks me to...
0
by: Darragh | last post by:
I'm encountering some serious problems trying to set Access 2000 querys (parameters and views) as data sources for my mail merge documents (Word 2003). After spending hours scouring the web for...
1
by: mr k | last post by:
Hi, I wanted to use mail merge with forms but Text form fields are not retained during mail merge in Word, I got the code from Microsoft but it doesn't remember the text form field options such as...
1
by: =?Utf-8?B?QmFkaXM=?= | last post by:
Hi, how can I use a dataset (as my datasource) to perform a Word Mail Merge? maybe methods like wrdMailMerge.OpenDataSource()!? or wrdMailMerge.DataSource...!? could help but I don't know how to...
7
by: giladp1 | last post by:
I found Albert Kallal's great "Super easy Word Merge" code in his site at: http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html Thanks Albert so much for sharing this. I am looking...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.