Solved

Basic Word Mail Merge functionality with C#.NET  - given Data File and Merge Doc

Posted on 2008-06-09
4
1,528 Views
Last Modified: 2013-12-17
Hello Experts,

I'm working with MS Office 2003 and visual studio 2005

I have experience with C# .NET and web applications, but I have not yet worked with Microsoft Office .. . until now.  I'm trying to understand how to automate a mail merge and am looking for a little guidance.

I have a simple Windows Application with a button. Given a data source file and and merge document, I would like to perform a merge with the OnClick event.

My merge document is a *.doc.
My data source is a *.xls.


I am hoping there is a method that takes 2 arguments: one for data source file, one for merge document.  Does this exist?  I'm having trouble understanding the documentation that I have found.  It seems all the examples create a data source file from scratch and I'm not understanding the call that actually merges the data.

Can someone point me in the right direction?

Thanks.

0
Comment
Question by:soapygus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21747414
Mail Merge uses a document to hold the pattern for the result documents. It is quite  easy to set it up manually using the Mail merge wizard which is built in to the Word user interface (Tools/Letters and Mailings). This requires that the datasource be specified and then the merge fields be placed where needed. The merge fields point to data source fields by name. If the main document, as it is called, is saved, the datasource link is saved with it.

Programatically you can use the OpenDataSource method to link a particular data source to the document. Hopefully it will have the same field names as the datasource that document was designed for.

A document object has a Mailmerge Property which holds the Datasource object and has an Execute method to actually run the merge.

I don't use C#, but if you can understand VB, the Help in Word's VBA editor should be able to help you more.
0
 

Author Comment

by:soapygus
ID: 21750633
Thanks GrahamSkan,

I've got it working with one small problem.  My OnClick event is referencing the correct files (template.doc, and datafile.xls); however, when it executes the merge I get a prompt asking me to "Select Table." My xls only has one worksheet so the worksheet is selected and a checkbox is checked that indicate that the first row is my header row.

I click "Ok" and the merge executes perfectly.

I just need to figure out how to specify the header row is my first record in the XLS.

Here's the code I've been working with.
            // Create an instance of Word  and make it visible.
            wrdApp = new Word.Application();
            wrdApp.Visible = true;
            Word.Document myMergeDocument;

            string strFileName = "c:\\template.doc";
            string strDataFile = "c:\\datafile.xls";

            object filename = strFileName;
            object objTrue = true;
            object objFalse = false;
            object objMiss = Type.Missing;


            //Open merge document
            myMergeDocument = wrdApp.Documents.Open(ref filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
            myMergeDocument.Select();

           

            //Open the data source
            object source = strDataFile;
            object format = Word.WdOpenFormat.wdOpenFormatAuto;
            myMergeDocument.MailMerge.OpenDataSource(strDataFile, ref format, ref objFalse, ref objMiss, ref objTrue, ref objFalse, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);

            //Perform Merge
            myMergeDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;

           
            myMergeDocument.MailMerge.SuppressBlankLines = true;
            myMergeDocument.MailMerge.DataSource.FirstRecord = (int)Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
            myMergeDocument.MailMerge.DataSource.LastRecord = (int)Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
            myMergeDocument.MailMerge.Execute(ref objFalse);
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 21753094
Try putting

"SELECT * FROM `Sheet1$`"

as the 13th parameter in the OpenDataSource statement
0
 

Author Closing Comment

by:soapygus
ID: 31465563
Thank you!
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question