sept. 06

Some time ago, I have published a serie of article speaking about VSTO, .NET 3.5 and Excel. A few time ago, I wanted to merge a word document with a CSV file.

Let's see step by step how to achieve this:

Let's create a .csv file

  • Create a simple text file on which you will put
    • Some key names on the first line
    • Some values in the second line
  • Save it (let's say c:\temp\merge\datafile.txt)

This file will be the one that will be merged with your word template.

Let's create your template word document

  • Open Word (I will give the instructions following Word 2007)
  • Click on "Mailings"
  • Click on "Start Mail Merge" and "Letter"
  • Click on "Select Recipients"
  • Click on "Use an existing Data Source File" and select the file you have just created in the previous step
  • Just write down your document and when you want to insert one of the fields that is present in the document just do the following :
    • Click on "Mailings"
    • Click on "Insert a merge field" and select the field you want to add
  • Save the document (let's say c:\temp\merge\doc to merge.docx)

Let's now do the merge programmatically

  • Create a new Console Application
  • Add a reference to the following DLLs:
    • Microsoft.Office.Interop.Word.dll
    • Microsoft.Office.Tools.Word.v9.0.dll 

The code is quite clear but here are some additional comments to help understanding some parts:

using System;
using Microsoft.Office.Interop.Word;
 
class Program
{
   static object missing = Type.Missing;
 
   static void Main(string[] args)
   {
      //1. Get the winword application
      _Application application = (_Application)new Application();
 
      //2. Open the template document
      object fileName = @"C:\Temp\Merge\doc to merge.docx";
      _Document document = application.Documents.Open(ref fileName, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
 
      //3. Prepare the Mail Merge
      document.MailMerge.MainDocumentType = WdMailMergeMainDocType.wdFormLetters;
      document.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument;
 
      //4. Open the data source (recipents)
      string dataSource = @"C:\Temp\Merge\datafile.txt";
      object subType = WdMergeSubType.wdMergeSubTypeOther;
      document.MailMerge.OpenDataSource(dataSource, ref missing, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing, ref subType);
 
      //5. Execute the MailMerge
      document.MailMerge.Execute(ref missing);
 
      //6. Close the template without saving
      object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
      document.Close(ref saveChanges, ref missing, ref missing);
 
      //7. Get a reference to the newly created document 
      //   (it will be the only opened document, ie, index = 1)
      object index = 1;
      document = application.Documents.get_Item(ref index);
 
      //8. Save the newly created document
      object newFileName = @"C:\Temp\Merge\doc merged.docx";
      document.SaveAs(ref newFileName, ref missing, ref missing, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing);
      document.Close(ref missing, ref missing, ref missing);
 
      //9. Close Winword
      object saveOptions = WdSaveOptions.wdDoNotSaveChanges;
      application.Quit(ref saveOptions, ref missing, ref missing);
   }
}
  • I use the _Application and _Document class instead of Application and Document to avoid having some warning due to ambiguous calls.
  • Yes, Application is an interface. So why do we do a new on an interface ? Simply because we are dealing with COM objects
  • The WORD object model uses only by reference parameters. However there is no problelm declaring a static variable as a shortcut to Type.Missing
  • All collection in the office object model are 1-based (contrary to the .NET collections that are all 0-based)
  • You should of course be a lot more cautious when using this code in production mode : be sure that the Close and Quit methods get called, otherwise, you will get many processes running in background !
  • If you want to see the application running, just do application.visible = true

What does it give ?

Tags:

Commentaires

Busby SEO Test Freak

Posted on jeudi, 29 janvier 2009 22:40

merci pour l'information.
il est très utile.

Jason Gerstorff

Posted on jeudi, 26 février 2009 17:09

Thanks very helpful. You did have one error though. Although you are declaring the new file name "doc Merged.docx", by default the Document.SaveAs method saves the file in compatibility mode. (At least it did on my computer.)  I was getting an error trying to open the merged file and couldn't figure out why. I believe Word was expecting 07 format from the extension, but getting '97 format.

If you set the second parameter on the SaveAs() it fixes the issue.

object saveFormat = WdSaveFormat.wdFormatDocumentDefault;
document.SaveAs(ref newFileName, ref saveFormat, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing);
            document.Close(ref missing, ref missing, ref missing);

Thanks!

Jack

Posted on samedi, 12 novembre 2011 02:49

How to add attachment with the mailmerge process?

Jack

Posted on samedi, 12 novembre 2011 02:50

How to add attachment to the mailmerge?

seo software programs

Posted on dimanche, 28 juillet 2013 18:32

You are so interesting! I do not suppose I have read through something like this before. So wonderful to discover somebody with some genuine thoughts on this subject. Really.. thanks for starting this up. This web site is something that's needed on the web, someone with a bit of originality!

my site;  seo software programs - http://topseosoft.com/senuke-xcr/

best seo companies

Posted on jeudi, 1 août 2013 05:36

Admiring the persistence you put into your website and detailed information you present. It's awesome to come across a blog every once in a while that isn't the same old rehashed information. Fantastic read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.

Also visit my website;  best seo companies - http://qualified-seo.com/

Felicia

Posted on mercredi, 21 août 2013 00:40

What's up, yeah this post is truly pleasant and I have learned lot of things from it regarding blogging. thanks.

Also visit my site - recetas para bajar de peso ( Felicia - impotenceanddiabeteshelp.com/.../ )

Engine Optimization Search Seo Software

Posted on dimanche, 1 septembre 2013 06:17

Using a professional SEO agency to build a structure and implementing a keyword-rich site survey is conducted can have an immediate effect when the site goes live. But make sure that you are hiring right seo company to gain the success that you desire for. There are many places online where you can learn how to do simple seo techniques that will permit you to benefit from optimization techniques.

My web-site ::  Engine Optimization Search Seo Software - http://scrapeboxvps.ca

prada vitello shine 長財布

Posted on lundi, 23 décembre 2013 01:34

prada vitello shine 長財布 - http://www.zivelahrana.com/carica/purada.php?1154  This is my first time go to see at here and i am really impressed to read all at one place.  prada vitello shine 長財布 - http://www.zivelahrana.com/carica/purada.php?1154

Natural Green Cleanse

Posted on vendredi, 27 décembre 2013 02:06

Your way of explaining the whole thing in this piece of writing is genuinely fastidious, all be able to effortlessly understand it, Thanks a lot.

my weblog -  Natural Green Cleanse - melody8.mindpia.com/

detox cleansing diet

Posted on lundi, 6 janvier 2014 11:02

It's hard to come by well-informed people in this particular topic, however, you seem like you know what you're talking about! Thanks

Have a look at my site;  detox cleansing diet - http://www.talentmoments.gr/users/JasperWat

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading