T
Todd
Hi,
I'm trying to integrate a mail-merge into our inventory control
application, but without success.
I'm using Word 2002 and the data is coming from SQL Server 2k.
The application is a C# windows forms app.
Basically I'm getting an exception "Word was unable to open the data
source" when I make a call to MailMerge.OpenDataSource.
The frustrating thing is that it works fine when I do it myself in
Word. I use exactly the same .ODC file (created by Word) in the
application. The SQL queries work fine on their own too.
I've tried recording a macro, then translating the VBA into C#, but to
no avail. It really is just a couple of function calls - arghhh!
I think I've tried just about every conceivable parameter combination
but without success. Using http://support.microsoft.com/?kbid=285332
as a guide, the code is as follows:
private void DoMailMergeToPrinter
(
object oTemplate, // path\filename of the Word envelope template
string datasource, // path\filename of the .odc file created by
Word
object query // SQL query string
)
{
object oFalse = false;
object oTrue = true;
object emptyString = "";
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass ();
Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
Word.MailMergeFields wrdMergeFields;
try
{
Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref
missing,ref missing, ref missing);
oWordDoc.Activate ();
wrdMailMerge = oWordDoc.MailMerge;
wrdSelection = oWordApp.Selection;
wrdMergeFields = wrdMailMerge.Fields;
wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdEnvelopes;
wrdMailMerge.OpenDataSource (datasource, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing,
ref query,
ref missing, ref missing, ref missing);
wrdMailMerge.SuppressBlankLines = true;
wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToPrinter;
wrdMailMerge.Execute (ref oFalse);
}
catch (Exception ex)
{
MessageBox.Show (this, ex.Message, "Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
oWordApp.Application.Quit (ref oFalse, ref missing, ref missing);
}
}
I thought perhaps it might be a security issue, but I've tried logging
in as "sa" to no avail. I'm using the same .ODC file anyway, so if it
works manually, it should also work when automated.
Please help!
Thank you,
Todd.
I'm trying to integrate a mail-merge into our inventory control
application, but without success.
I'm using Word 2002 and the data is coming from SQL Server 2k.
The application is a C# windows forms app.
Basically I'm getting an exception "Word was unable to open the data
source" when I make a call to MailMerge.OpenDataSource.
The frustrating thing is that it works fine when I do it myself in
Word. I use exactly the same .ODC file (created by Word) in the
application. The SQL queries work fine on their own too.
I've tried recording a macro, then translating the VBA into C#, but to
no avail. It really is just a couple of function calls - arghhh!
I think I've tried just about every conceivable parameter combination
but without success. Using http://support.microsoft.com/?kbid=285332
as a guide, the code is as follows:
private void DoMailMergeToPrinter
(
object oTemplate, // path\filename of the Word envelope template
string datasource, // path\filename of the .odc file created by
Word
object query // SQL query string
)
{
object oFalse = false;
object oTrue = true;
object emptyString = "";
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass ();
Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
Word.MailMergeFields wrdMergeFields;
try
{
Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref
missing,ref missing, ref missing);
oWordDoc.Activate ();
wrdMailMerge = oWordDoc.MailMerge;
wrdSelection = oWordApp.Selection;
wrdMergeFields = wrdMailMerge.Fields;
wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdEnvelopes;
wrdMailMerge.OpenDataSource (datasource, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing,
ref query,
ref missing, ref missing, ref missing);
wrdMailMerge.SuppressBlankLines = true;
wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToPrinter;
wrdMailMerge.Execute (ref oFalse);
}
catch (Exception ex)
{
MessageBox.Show (this, ex.Message, "Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
oWordApp.Application.Quit (ref oFalse, ref missing, ref missing);
}
}
I thought perhaps it might be a security issue, but I've tried logging
in as "sa" to no avail. I'm using the same .ODC file anyway, so if it
works manually, it should also work when automated.
Please help!
Thank you,
Todd.