3

I would like to automate printing invoices from a Word Template. I think I have the logic down but I just don't know the right way of coding it. Here is my current code

Selection wrdSelection;
MailMerge wrdMailMerge;
MailMergeFields wrdMergeFields;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Document wrdDoc = new Document();
wordApp.Visible = false;
wrdSelection = wordApp.Selection;

object oMissing = System.Reflection.Missing.Value;

// PUT MY EXISTING TEMPLATE FILE INTO WORD DOCUMENT
wrdDoc = wordApp.Documents.Add(Properties.Resources.invoiceTemp,oMissing,oMissing, oMissing);
// RETREIVE MAIL MERGE PROPERTIES FROM THE DOCUMENT IN HOPES OF UTILIZING IT

wrdMailMerge = wrdDoc.MailMerge;
wrdDoc.Select();
wrdMergeFields = wrdMailMerge.Fields;

If it would help, here are the Merge Fields that I have on my template:

date_issued, month_covered, invoiceNo, tuition, lunchFee, discount, studentNo, studentName, amountDue, amountPaid, balance, penalty, status

Now, how do I add data that I retrieve using my Application into the document which gets all the properties from the template?

Bassie
  • 9,529
  • 8
  • 68
  • 159
John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71
  • 1
    It seems you did not read [the reference](http://support.microsoft.com/kb/301659) I provided on your [previous question](http://stackoverflow.com/questions/13180654/how-to-do-a-mail-merge-in-c-sharp-using-interop-word/13183814#13183814). Furthermore, in that question you accepted an answer that recommended you create a document based on the template. The template should already include the relevant merge fields. – Fionnuala Nov 02 '12 at 10:28
  • Yes, but I still don't know how to add/change the mergefields to contain the data to be printed. So I went another route – John Ernest Guadalupe Nov 02 '12 at 11:17
  • 1
    Did you read the link? It says, amongst a great many other things `wrdMergeFields.Add(wrdSelection.Range, "Address");` – Fionnuala Nov 02 '12 at 11:19
  • That adds a merge field. I already have a mergefield set in the template. There seems to be not a function there that shows how to select a merge field and change its contents. Thanks for sharing it though I did learn the logic – John Ernest Guadalupe Nov 02 '12 at 11:21
  • 1
    The contents of a merge field comes from the linked data. To change the contents, change the linked data. It does not show how to select and change the contents of a merge field in the document, because then you do not have a merge. What exactly are you trying to do? – Fionnuala Nov 02 '12 at 11:27
  • I would like to change put information on the merge field that I retrieve from a database in my application. So when the print button it clicked it would retrieve the data to be printed on the invoice and then put the data on the merge fields and then print it. I'm really being confused here sorry – John Ernest Guadalupe Nov 02 '12 at 11:38
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18962/discussion-between-remou-and-john-ernest-guadalupe) – Fionnuala Nov 02 '12 at 12:00

1 Answers1

3

Here are some notes on running with a template and data file.

Word.Application _wordApp = new Word.Application();
Word.Document oDoc = _wordApp.Documents.Add(@"z:\docs\mergetemplate.dotx");
_wordApp.Visible = true;
oDoc.MailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdFormLetters;
oDoc.MailMerge.OpenDataSource(@"Z:\Docs\new.csv", false, false, true);
oDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
oDoc.MailMerge.Execute(false);

The newly created merge file is now the active document, so you can save it:

Word.Document oLetters = _wordApp.ActiveDocument;
oLetters.SaveAs2(@"z:\docs\letters.docx", 
     Word.WdSaveFormat.wdFormatDocumentDefault);
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • Would I need to rename the text file we were talking about earlier into a csv file? – John Ernest Guadalupe Nov 02 '12 at 14:08
  • 1
    A csv is a text file. I suggest you use a csv (comma separated value) for your data. An output from Access would look something like `DoCmd.TransferText acExportDelim, , "Table1_or_Query1", "z:\docs\data.csv", True`. It does not much matter what it is called, within certain limits, and you can set up the name as a variable if you wish. You can even name the files by date, if you want a record. – Fionnuala Nov 02 '12 at 14:12
  • I did it Remou! It adds the data, but how do I keep it from opening all sorts of windows and asking me to save the documents? I'd like to discard all the documents created retaining only the template file? – John Ernest Guadalupe Nov 02 '12 at 14:50
  • 1
    The document created is your letters. You want to close the template file and ask the user if they want to save or print the document created. – Fionnuala Nov 02 '12 at 14:54
  • Okay so how do I address the new Document? I mean where would it be located so that I could .PrintOut() it? – John Ernest Guadalupe Nov 02 '12 at 14:57
  • Got it now Remou! IT is done by oDoc.MailMerge.Application.PrintOut(); – John Ernest Guadalupe Nov 02 '12 at 15:04
  • Thanks a lot for everything!\ – John Ernest Guadalupe Nov 02 '12 at 15:05