B
Bathan
Hi, im trying to do a simple MailMerge with a txt datasource using C#
w/reflection.
I found some code over the net but its all for VB6 , ive managed to
transalte it but something's not working.
I have 20 records on my txt and when i do the mailmerge, it builds the
labels but does not fill it up with the recipients.
Here is the code i managed to translate:
object oMissing = Missing.Value;
Microsoft.Office.Interop.Word._Application oApp = new
Microsoft.Office.Interop.Word.ApplicationClass();
_Document oDoc = null;
// Create new Word Application
oApp.Visible = true;
oDoc = oApp.Documents.Add(ref oMissing,ref oMissing,ref oMissing,ref
oMissing);
oDoc.MailMerge.Fields.Add(oApp.Selection.Range,"Name");
oApp.Selection.TypeParagraph();
oDoc.MailMerge.Fields.Add(oApp.Selection.Range,"Address");
oApp.Selection.TypeParagraph();
Word.AutoTextEntry oAutoText =
oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout",oDoc.Content);
//oDoc.Content.Delete(ref oMissing,ref oMissing);
oDoc.MailMerge.MainDocumentType =
WdMailMergeMainDocType.wdMailingLabels;
oDoc.MailMerge.OpenDataSource(@"c:\data.txt",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);
object tch = "5160";
object lay = "MyLabelLayout";
oApp.MailingLabel.CreateNewDocument(ref tch,ref oMissing,ref lay,ref
oMissing,ref oMissing,ref oMissing,ref oMissing);
oDoc.MailMerge.Destination =
WdMailMergeDestination.wdSendToNewDocument;
object oFalse = false;
oDoc.MailMerge.Execute(ref oFalse);
oDoc.Saved = true;
oAutoText.Delete();
oDoc.Close(ref oMissing,ref oMissing,ref oMissing);
oApp.Visible = true;
oApp.NormalTemplate.Saved = true;
As you can see, its pretty "basic" , i wanna get the hang of it before
y get into detail.
My TXT has 20 records tab separated and linefeed for the row change.
The weird thing is that the number of labels generated are OK, but they
are empty (
Any help ?
w/reflection.
I found some code over the net but its all for VB6 , ive managed to
transalte it but something's not working.
I have 20 records on my txt and when i do the mailmerge, it builds the
labels but does not fill it up with the recipients.
Here is the code i managed to translate:
object oMissing = Missing.Value;
Microsoft.Office.Interop.Word._Application oApp = new
Microsoft.Office.Interop.Word.ApplicationClass();
_Document oDoc = null;
// Create new Word Application
oApp.Visible = true;
oDoc = oApp.Documents.Add(ref oMissing,ref oMissing,ref oMissing,ref
oMissing);
oDoc.MailMerge.Fields.Add(oApp.Selection.Range,"Name");
oApp.Selection.TypeParagraph();
oDoc.MailMerge.Fields.Add(oApp.Selection.Range,"Address");
oApp.Selection.TypeParagraph();
Word.AutoTextEntry oAutoText =
oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout",oDoc.Content);
//oDoc.Content.Delete(ref oMissing,ref oMissing);
oDoc.MailMerge.MainDocumentType =
WdMailMergeMainDocType.wdMailingLabels;
oDoc.MailMerge.OpenDataSource(@"c:\data.txt",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);
object tch = "5160";
object lay = "MyLabelLayout";
oApp.MailingLabel.CreateNewDocument(ref tch,ref oMissing,ref lay,ref
oMissing,ref oMissing,ref oMissing,ref oMissing);
oDoc.MailMerge.Destination =
WdMailMergeDestination.wdSendToNewDocument;
object oFalse = false;
oDoc.MailMerge.Execute(ref oFalse);
oDoc.Saved = true;
oAutoText.Delete();
oDoc.Close(ref oMissing,ref oMissing,ref oMissing);
oApp.Visible = true;
oApp.NormalTemplate.Saved = true;
As you can see, its pretty "basic" , i wanna get the hang of it before
y get into detail.
My TXT has 20 records tab separated and linefeed for the row change.
The weird thing is that the number of labels generated are OK, but they
are empty (
Any help ?