Re: C# MailMerge from Excel

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



When you've finsihed with the Word object and released oWordDoc etc, you'll
need something like

oWordApp.Quit

Peter Jamieson


"chris" <chris@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5DC067F7-6635-418C-BC80-89819DAFED07@xxxxxxxxxxxxxxxx
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.ReleaseComObject (wrdMailMerge);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWordApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWordDoc);

or like this:

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

but none of those ways worked. any tricks?

Thanks!



"Cindy M -WordMVP-" wrote:

Hi =?Utf-8?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$`";

wrdMailMerge.OpenDataSource(@"C:\test\list.xls",ref 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 :-)




.



Relevant Pages

  • Re: C# MailMerge from Excel
    ... oWordDoc.SaveAs(ref oTemplateDone,ref missing, ref missing, ref ... oFalse, ref oTrue,ref oFalse, ref missing, ref missing,ref oFalse, ref ... Word.MailMerge wrdMailMerge; ...
    (microsoft.public.word.mailmerge.fields)
  • Re: which One is Better
    ... I have got some inputs from some of people who worked on add-in project. ... by what versions of Outlook you're targeting, what versions of Windows, ... this.CheckSpelling(ref missing, ref missing, ref missing,ref missing, ref ...
    (microsoft.public.office.developer.com.add_ins)
  • Re: C# MailMerge from Excel
    ... oFalse, ref oTrue,ref oFalse, ref missing, ref missing,ref oFalse, ref ... missing, ref missing,ref oConn, ref oQuery, ref missing, ref missing,ref ... This reply is posted in the Newsgroup; please post any follow question or reply ...
    (microsoft.public.word.mailmerge.fields)
  • Re: C# MailMerge from Excel
    ... oWordApp.ActiveDocument.SaveAs(ref oTemplateDone,ref missing, ref missing, ... ref missing,ref missing, ref missing, ref missing,ref missing, ref missing, ... // Unload objects from the memory ... oFalse, ref oTrue,ref oFalse, ref missing, ref missing,ref oFalse, ref ...
    (microsoft.public.word.mailmerge.fields)
  • Microsoft.Office.Interop.Word stops working after inserting a pict
    ... 11 COM Object Library to replace data fields in a .dot template. ... missing, ref missing, ref isVisible); ...
    (microsoft.public.office.developer.automation)