Click here to Skip to main content
Licence CPOL
First Posted 26 Apr 2006
Views 37,882
Bookmarked 14 times

C# Word 2003 Mailmerge by DSN

By | 26 Apr 2006 | Article
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 :)

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)

About the Author

Digital_Human

Web Developer

Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUsing a Doc and not a DOT Pinmemberriccarrasquilla9:28 28 Sep '09  
GeneralDSN Setting Pinmemberriccarrasquilla9:26 28 Sep '09  
QuestionKeep getting an error on OpenDataSource...... Pinmemberdan180885:16 19 Mar '08  
AnswerRe: Keep getting an error on OpenDataSource...... Pinmembersp3c139:21 5 May '08  
GeneralRe: Keep getting an error on OpenDataSource...... PinmemberLYH1232:18 29 Aug '08  
GeneralMailmerge doesn't work with SQL DSN PinmemberMember #47720110:08 27 Dec '06  
GeneralRe: Mailmerge doesn't work with SQL DSN PinmemberErzsebet Szabo7:25 30 Jan '07  
GeneralRe: Mailmerge doesn't work with SQL DSN PinmemberLYH1231:59 29 Aug '08  
GeneralMissing code PinmemberMike Prather3:57 12 May '06  
GeneralRe: Missing code PinmemberDigital_Human4:21 12 May '06  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 27 Apr 2006
Article Copyright 2006 by Digital_Human
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid