Word.Document WordDoc; Word.MailMerge WordMailMerge;Then, instead of just opening the word document with the Open() and later using the ActiveDocument, we use what the Open() returns:
this.WordDoc = oWord.Documents.Open(ref oName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); this.WordMailMerge = this.WordDoc.MailMerge;But, wait ... now I see that there's one big missing step, that I guess I overlooked. You have to open the DataSource. Here's where I'll have difficulty helping you. It looks like we use an Excel table, but you're using a Text file. When opening the DataSource for the Excel, it looks something like this:
string FileName = "c:\\MyExcelFile.xls"; object Connection = "MailMergeTable"; object SqlQuery = "SELECT * FROM 'MailMergeTable$'"; object ReadOnly = true; this.WordMailMerge.OpenDataSource(FileName, ref oMissing, ref oMissing, ref ReadOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref Connection, ref SQLQuery, ref oMissing, ref oMissing, ref oMissing);And now you can finally execute the mailmerge. As I said, I'm not sure what the syntax for the .OpenDataSource() method should be if you're using a Text file rather than an Excel file, but I'm pretty sure that this is the step you were missing. Good luck!
>>oWord.ActiveDocument.MailMerge.Destination = 0;
>>oWord.ActiveDocument.MailMerge.Execute(ref oFalse);
>>
>>Our code does this instead:>>object Pause = 20000;
>>oWord.ActiveDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
>>oWord.ActiveDocument.MailMerge.Execute(ref Pause);
>>
>>I'm not sure if that's all that's wrong with your code, but try that and see if it helps.>>>// Create a Text file to do MailMerge >>>StreamWriter StrWriter = File.CreateText(@"K:\MYFOL\MAILMERG.TXT"); >>>StrWriter.WriteLine("isr_salut,isr_fname,isr_lname,isr_coname,isr_ad1,isr_ad2,isr_ad3,isr_ad4,is r_ad5"); >>>StrWriter.WriteLine("Mr.,Joe,Bloggs,Kernel Software,U6,Bridgecourt,Walkinstown Ave,walkinstown,Dublin,Ireland"); >>> StrWriter.Close(); >>> >>>oName = @"K:\MYFOL\MAILMERG.DOC"; >>>oWord.Documents.Open(ref oName, ref oMissing, >>> ref oMissing, ref oMissing, ref oMissing, ref oMissing, >>> ref oMissing, ref oMissing, ref oMissing, ref oMissing, >>> ref oMissing, ref oMissing, ref oMissing, ref oMissing, >>> ref oMissing, ref oMissing); >>> >>>oFormLetter = oWord.ActiveDocument; >>>oWord.ActiveDocument.MailMerge.Destination = 0; >>>// gives read only message >>>//oWord.ActiveDocument.MailMerge.DataSource.Name = "MAILMERG.TXT"; >>> >>> >>>// oWord.ActiveDocument.MailMerge.DataSource.FirstRecord = 1; >>>//oWord.ActiveDocument.MailMerge.DataSource.LastRecord = -16; >>>oWord.ActiveDocument.MailMerge.Execute(ref oFalse); >>>>>