Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

C# & Word Mail Merge Problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bishman - 20 Nov 2006 16:39 GMT
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);

}

--------------------------------------------------------------------------------------------------------------------------
Chester - 21 Nov 2006 02:47 GMT
Hi Bishman,

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

Cheers,
Chester

> Hi,
>
[quoted text clipped - 120 lines]
>
> --------------------------------------------------------------------------------------------------------------------------
Bishman - 21 Nov 2006 08:46 GMT
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

> Hi Bishman,
>
[quoted text clipped - 133 lines]
>>
>> --------------------------------------------------------------------------------------------------------------------------
mkoelmans@wanadoo.nl - 25 Nov 2006 13:56 GMT
Killing the app isnt all that diff:
wrdApp.Quit(ref oFalse, ref oMissing ,ref oMissing);

Bishman schreef:

> Yeah thanks, I have.
>
[quoted text clipped - 144 lines]
> >>
> >> --------------------------------------------------------------------------------------------------------------------------

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.