Click here to Skip to main content
15,893,594 members
Articles / Programming Languages / C#
Article

C# Word 2003 Mailmerge by DSN

Rate me:
Please Sign up or sign in to vote.
1.42/5 (9 votes)
26 Apr 2006CPOL2 min read 58.8K   16   11
Word 2003 Mailmerge using a DSN connection

Introduction<o:p>

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 <o:p>

<o:p> 

The start<o:p>

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 :) <o:p>

Now create the reference to Office Word 2003 v11.0 object. Now I have the code below to make things easyer.<o:p>

using Word = Microsoft.Office.Interop.Word;<o:p>

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.<o:p>

 <o:p>

The real thing<o:p>

Now lets create the code for our application Create the definitions:<o:p>

Word.Application wrdApp;<o:p>

Word.Document oDataDoc;<o:p>

Word.MailMerge wrdMailMerge;<o:p>

Object oMissing = System.Type.Missing;<o:p>

Object oFalse = false;<o:p>

Object oTrue = true;<o:p>

Object oName = Environment.CurrentDirectory + @"\template.dot";<o:p>

Object oFileName = Environment.CurrentDirectory + @"\saved.rtf";<o:p>

Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;<o:p>

<o:p> 

// Starting the Word Application<o:p>

wrdApp = new Word.ApplicationClass();<o:p>

wrdApp.Visible = true;<o:p>

wrdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize;<o:p>


// Open the template<o:p>

oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref oMissing, ref oMissing);<o:p>

oDataDoc.Activate();<o:p>

// Document 2 mailmerge format<o:p>

wrdMailMerge = oDataDoc.MailMerge;<o:p>

wrdMailMerge.HighlightMergeFields = false;

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

<o:p>

<o:p> 

The data part<o:p>

// The data part<o:p>

Object oConnection = "DSN=dsn_data"; //The DSN connection name<o:p>

Object oQuery = "SELECT * FROM some_table"; // The query to get data from the DSN<o:p>

Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;<o:p>

//Open the data source and merge the fields<o:p>

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);<o:p>

wrdMailMerge.SuppressBlankLines = true;<o:p>

<o:p> 

// Save document<o:p>

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);<o:p>

oDataDoc.Saved = true;<o:p>

<o:p> 

// Unload objects from the memory<o:p>

wrdMailMerge = null;<o:p>

oDataDoc = null;<o:p>

wrdApp = null;

License

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


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGenerate Multiple Document using Mail Merge. Pin
Mit Mehta15-Mar-13 2:15
Mit Mehta15-Mar-13 2:15 
GeneralUsing a Doc and not a DOT Pin
riccarrasquilla28-Sep-09 9:28
riccarrasquilla28-Sep-09 9:28 
GeneralDSN Setting Pin
riccarrasquilla28-Sep-09 9:26
riccarrasquilla28-Sep-09 9:26 
QuestionKeep getting an error on OpenDataSource...... Pin
dan1808819-Mar-08 5:16
dan1808819-Mar-08 5:16 
AnswerRe: Keep getting an error on OpenDataSource...... Pin
sp3c135-May-08 9:21
sp3c135-May-08 9:21 
GeneralRe: Keep getting an error on OpenDataSource...... Pin
LYH12329-Aug-08 2:18
LYH12329-Aug-08 2:18 
GeneralMailmerge doesn't work with SQL DSN Pin
michael359327-Dec-06 10:08
michael359327-Dec-06 10:08 
GeneralRe: Mailmerge doesn't work with SQL DSN Pin
Erzsebet Szabo30-Jan-07 7:25
Erzsebet Szabo30-Jan-07 7:25 
GeneralRe: Mailmerge doesn't work with SQL DSN Pin
LYH12329-Aug-08 1:59
LYH12329-Aug-08 1:59 
Check your dsn connection directly.
You can test it through admin tools->odbc
GeneralMissing code Pin
Mike Prather12-May-06 3:57
Mike Prather12-May-06 3:57 
GeneralRe: Missing code Pin
Digital_Human12-May-06 4:21
Digital_Human12-May-06 4:21 

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

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