Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
MS Word , MailMerge and C#
Message
De
13/03/2005 09:50:26
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00994745
Message ID:
00995291
Vues:
17
Hey Gerard,

Yeah, syntax is important here ... this stuff is tricky. I paraphrased somewhat from our working code ... it looked similar enough to yours and I thought maybe it didn't matter, but perhaps it does. Let me try again.

First, we declare a few fields:
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!

~~Bonnie


>Hi Bonnie.
>
>Thanks again for your reply and I appreciate your rummaging around for a solution even though its not 'your experise'
>
>I tried the code you suggested but am still getting an error up in the Execute line.... saying that the requested object is not available.
>
>I realise that syntax is very important here as I rememebr when I got this to work (eventually) in VFP, there was a lot of trial and error.
>
>Regards,
>Gerard
>
>
>
>>Gerard,
>>
>>As I said, I'm no expert ... but, in comparing your posted code to what I can figure out from our code, I notice a difference with the following few lines:
>>
>>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.
>>
>>~~Bonnie
>>
>>
>>
>>>Hi Bonnie
>>>Code is as follows:
>>
>>>// 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);
>>
>>>
>>>Following is happening:
>>>
>>>On the DataSource.Name line I was getting a Read Only Control, so I amended the doc file and inserted the link into the mail merge doc file itself
>>>(This works fine if I go into Word and run the merge fro there)
>>>
>>>I was getting errors with the First and Last record so I commented them
>>>It is also giving an error with the execute line, which I assume is the line that 'Does' the mailmerge
>>>
>>>Regards,
>>>Gerard
>>>
>>>
>>>
>>>
>>>
>>>>Gerard,
>>>>
>>>>How about a link to the demo you're talking about? Or, better yet, post your code. We've got MailMerge working with C#. I wasn't the one who wrote the code though, so I'm no expert, but I might be able to look at your code and see what the problem is.
>>>>
>>>>~~Bonnie
>>>>
>>>>
>>>>
>>>>>Hi.
>>>>>I have been trying to get Mailmerge automation up with c #.
>>>>>I've got it to the stage where it loads up Word, and opens the document.
>>>>>Howeverwhen I run the Execute method ,as follows:
>>>>>
>>>>>oWord.ActiveDocument.MailMerge.Execute(ref oFalse);
>>>>>
>>>>>I get a .com exception error.
>>>>>
>>>>>(I use same format as per demo on MS site)
>>>>>
>>>>>Wondering if anybody here has got Mailmerge working with c#
>>>>>
>>>>>Regards,
>>>>>Gerard
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform