View Single Post
  #8   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Chris Chris is offline
external usenet poster
 
Posts: 237
Default C# MailMerge from Excel

Thank you! I got it to work like this:

wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref missing);

object oTemplateDone = @"C:\test\done.doc";

oWordApp.ActiveDocument.SaveAs(ref oTemplateDone,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);

After the merge is done I still have WINWORD.EXE running under aspnet. I
know that GC is supposed to take care of that, but I have it in memory for a
long period of time. I tried to release them like this:

// Unload objects from the memory
System.Runtime.InteropServices.Marshal.ReleaseComO bject (wrdMailMerge);
System.Runtime.InteropServices.Marshal.ReleaseComO bject (oWordApp);
System.Runtime.InteropServices.Marshal.ReleaseComO bject (oWordDoc);

or like this:

wrdMailMerge = null;
oWordDoc = null;
oWordApp = null;

but none of those ways worked. any tricks?

Thanks!



"Cindy M -WordMVP-" wrote:

Hi ?B?Y2hyaXM=?=,

OK, my code executes with no errors, and it doesn't hang, but nothing really
happens.

object oConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\test\\list.xls;Mode=Read;Extended
Properties=\"HDR=YES;IMEX=1;\";Jet OLEDB:Engine Type=35;";

object oQuery = "SELECT * FROM `Sheet1$`";

",re f oFormat,ref oFalse, ref
oFalse, ref oTrue,ref oFalse, ref missing, ref missing,ref oFalse, ref
missing, ref missing,ref oConn, ref oQuery, ref missing, ref missing,ref
oSubType);

the code runs with the connection you told me, but I am not sure it is
reading from the file.

Comment everything after this point, make sure the Word app is visible and
activated. Display the mail merge toolbar and see if any besides the first two
buttons are available. Are you able to see the list of fields, for example? Or
to preview the data (the ABC button)?

To answer your question: the new document is generated only in memory and
should have become the ActiveDocument. It's not saved to disk. If you aren't
seeing it, then Execute is apparently not running. Try passing an object that's
set to false (object ofalse = false; wrdMailmerge.Execute(ref ofalse) and see
if that makes any difference.

after I call OpenDataSource() I do this:

wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref missing);

the enum WdMailMergeDestination.wdSendToNewDocument sends the merge doc to a
new file. where is that doc generated? in the same directory?

Since nothing happened, I tried saving it:

object oTemplateDone = @"C:\test\done.doc";

oWordDoc.SaveAs(ref oTemplateDone,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);

The doc is saved as done.doc with the merge fields not populated.


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)