Click here to Skip to main content
Click here to Skip to main content

C# Word 2003 Mailmerge by DSN

, 26 Apr 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Word 2003 Mailmerge using a DSN connection

Introduction

Well most of u guys already know the basic stuff, so i go from the 'Start Word Application Class'. This code will start Word 2003, create a mailmerge document and will merge the data recieved from a DSN source to this document

 

The start

Create a Word template (.dot) file with mergefields like i did. Or use a .doc file, whatever you want. My mergefields are called 'bedrijfsnaam','adres' and so on. Its dutch so forget about the names Smile | :)

Now create the reference to Office Word 2003 v11.0 object. Now I have the code below to make things easyer.

using Word = Microsoft.Office.Interop.Word;

Now create a User-DSN with the name 'dsn_data'. Thats what i dit so you'll be able to find the name back in the code. This connection can be to any datasource you want.

 

The real thing

Now lets create the code for our application Create the definitions:

Word.Application wrdApp;

Word.Document oDataDoc;

Word.MailMerge wrdMailMerge;

Object oMissing = System.Type.Missing;

Object oFalse = false;

Object oTrue = true;

Object oName = Environment.CurrentDirectory + @"\template.dot";

Object oFileName = Environment.CurrentDirectory + @"\saved.rtf";

Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;

 

// Starting the Word Application

wrdApp = new Word.ApplicationClass();

wrdApp.Visible = true;

wrdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize;


// Open the template

oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref oMissing, ref oMissing);

oDataDoc.Activate();

// Document 2 mailmerge format

wrdMailMerge = oDataDoc.MailMerge;

wrdMailMerge.HighlightMergeFields = false;

wrdMailMerge.ViewMailMergeFieldCodes = 0; //this is for showing the data instead of fieldnames

 

The data part

// The data part

Object oConnection = "DSN=dsn_data"; //The DSN connection name

Object oQuery = "SELECT * FROM some_table"; // The query to get data from the DSN

Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;

//Open the data source and merge the fields

wrdMailMerge.OpenDataSource("", ref oFileFormat, ref oMissing, ref oMissing, ref oTrue, ref oMissing,ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oConnection, ref oQuery, ref oMissing, ref oMissing, ref oSubType);

wrdMailMerge.SuppressBlankLines = true;

 

// Save document

oDataDoc.SaveAs(ref oFileName, ref oFileFormat, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

oDataDoc.Saved = true;

 

// Unload objects from the memory

wrdMailMerge = null;

oDataDoc = null;

wrdApp = null;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Digital_Human
Web Developer
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
QuestionGenerate Multiple Document using Mail Merge. PinmemberMit Mehta15-Mar-13 3:15 
GeneralUsing a Doc and not a DOT Pinmemberriccarrasquilla28-Sep-09 10:28 
GeneralDSN Setting Pinmemberriccarrasquilla28-Sep-09 10:26 
QuestionKeep getting an error on OpenDataSource...... Pinmemberdan1808819-Mar-08 6:16 
AnswerRe: Keep getting an error on OpenDataSource...... Pinmembersp3c135-May-08 10:21 
GeneralRe: Keep getting an error on OpenDataSource...... PinmemberLYH12329-Aug-08 3:18 
GeneralMailmerge doesn't work with SQL DSN PinmemberMember #47720127-Dec-06 11:08 
GeneralRe: Mailmerge doesn't work with SQL DSN PinmemberErzsebet Szabo30-Jan-07 8:25 
GeneralRe: Mailmerge doesn't work with SQL DSN PinmemberLYH12329-Aug-08 2:59 
GeneralMissing code PinmemberMike Prather12-May-06 4:57 
GeneralRe: Missing code PinmemberDigital_Human12-May-06 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 27 Apr 2006
Article Copyright 2006 by Digital_Human
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid