I'm still very new to c# and my programing skills are still basic.
I want to specify a start number in textBox1 and specify an end number in textBox2.
I have a .docx document which has set out spaces for label mail merge with 154 labels per sheet.
I want to click button1 to open the .docx document (named testMerge.docx and located in C:\) and have it populated with the start number e.g. 1001 and then the nextRecord should be 1002 and so on untill a maximum amount of labels had numbers on them before it went onto another page.
I have searched for hours and haven't found any simple code that i can understand.
Can someone show me how this can be done.
Many thanks.
Simple Mail Merge
7 Answers
Know the answer? Post it. Somebody with the same question will find it here.
cory thompson17 pointsPosted Dec 22, 2011, 3:46 AM EST
cory thompson17 pointsPosted Dec 26, 2011, 3:10 PM EST
John Glenn26 pointsPosted Feb 15, 2012, 3:07 AM EST
I am not sure what you exactly need because I don't have your testMerge.docx template file, but you should try using this C# Word library.
Here is how your C# Mail Merge code could look like:
// Creates testMerge.docx document. var document = new DocumentModel(); document.Sections.Add( new Section(document, new Paragraph(document, new Run(document, "Label no. "), new Field(document, FieldType.MergeField, "RangeStart:Numbers"), new Field(document, FieldType.MergeField, "Number"), new Field(document, FieldType.MergeField, "RangeEnd:Numbers")))); // Instead use: // var document = DocumentModel.Load(@"C:\testMerge.docx", LoadOptions.DocxDefault); // Initialize data source for a mail merge. var numbers = Enumerable.Range(startNumber, endNumber - startNumber + 1). Select(number => new { Number = number }); // Perform a mail merge. document.MailMerge.ExecuteRange("Numbers", numbers); // Save document to a file. document.Save("Document.docx", SaveOptions.DocxDefault);cory thompson17 pointsPosted Feb 17, 2012, 2:25 PM EST
int startNumber;
int endNumber;
Int32.TryParse(tboxStartNumber.Text, out startNumber);
Int32.TryParse(tboxEndNumber.Text, out endNumber);
var numbers = Enumerable.Range(startNumber, endNumber - startNumber + 1).Select(number => new { Number = number });
Object oMissing = System.Reflection.Missing.Value;
Object oTrue = true;
Object oFalse = false;
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
oWord.Visible = true;
oWordDoc = oWord.Documents.Open("C:\\Users\\Me\\Documents\\test.docx");
foreach(var num in numbers)
{
"INSERT MISSING CODE HERE TO FILL THE DOCX FILE"(num.Number.ToString());
}
cory thompson17 pointsPosted Feb 17, 2012, 11:55 PM EST
John Glenn26 pointsPosted Feb 19, 2012, 11:34 PM EST
Now I understand what you want to accomplish, and here is an exact C# Word code how you should do it:
// Use the component in free mode. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); // Creates testMerge.docx document. var document = new DocumentModel(); document.Sections.Add( new Section(document, new Paragraph(document, new Field(document, FieldType.MergeField, "RangeStart:Numbers"), new Field(document, FieldType.MergeField, "Number"), new Field(document, FieldType.MergeField, "RangeEnd:Numbers")))); // Instead use: // var document = DocumentModel.Load(@"C:\testMerge.docx", LoadOptions.DocxDefault); // Initialize data source for a mail merge. var numbers = Enumerable.Range(startNumber, endNumber - startNumber + 1). Select(number => new { Number = number }).ToList(); // Add a dummy number after every sequence of 154 numbers. for (int i = 154; i < numbers.Count; i += 154) { numbers.Insert(i, new { Number = -1 }); ++i; } // Mail merge range will continue expanding in the same paragraph, not new paragraph. document.MailMerge.RangeResolution = MailMergeRangeResolution.SiblingAncestors; // Handle field merging event to replace a merge field with a page break on every 155-th number (that is a dummy number we inserted before) // and for all other numbers, replace it with a number value. document.MailMerge.FieldMerging += (sender, e) => { if (e.RecordNumber % 155 == 0) e.Inline = new SpecialCharacter(document, SpecialCharacterType.PageBreak); else e.Inline = new Run(document, string.Format("{0} ", e.Value)); }; // Perform a mail merge. document.MailMerge.ExecuteRange("Numbers", numbers); // Save document to a file. document.Save("Document.docx", SaveOptions.DocxDefault);This code doesn't use C# Word Interop, instead it uses this C# / VB.NET Word library.
cory thompson17 pointsPosted Feb 21, 2012, 9:40 AM EST
Thanks for your input, but I wanted to use Word Interop. I didn't want to buy any software for something that can be done another way. and its expensive for personal use.
Below is where i am at but its not filling correctly.
This is how it appears.
int startNumber; int endNumber; Int32.TryParse(tboxStartNumber.Text, out startNumber); Int32.TryParse(tboxEndNumber.Text, out endNumber); // Initialize data source for a mail merge. var numbers = Enumerable.Range(startNumber, endNumber - startNumber + 1).Select(number => new { Number = number }); //OBJECT OF MISSING "NULL VALUE" Object oMissing = System.Reflection.Missing.Value; //OBJECTS OF FALSE AND TRUE Object oTrue = true; Object oFalse = false; //CREATING OBJECTS OF WORD AND DOCUMENT Word.Application oWord = new Word.Application(); Word.Document oWordDoc = new Word.Document(); //MAKING THE APPLICATION VISIBLE oWord.Visible = true; //OPEN THE FILE oWordDoc = oWord.Documents.Open(path + "\\Template.doc"); //oWordDoc.MailMerge.OpenDataSource(path + "\\Template.doc"); Word.MailMerge wrdMailMerge; Word.Selection wrdSelection; Word.MailMergeFields wrdMergeFields; wrdSelection = oWord.Selection; wrdMailMerge = oWordDoc.MailMerge; wrdMergeFields = wrdMailMerge.Fields; foreach (var num in numbers) { //wrdMailMerge.Fields.AddFillIn(wrdSelection.Range, num.Number.ToString()); wrdMailMerge.Fields.Add(wrdSelection.Range,num.Number.ToString()); //wrdMergeFields.Add(wrdSelection.Range, num.Number.ToString()); //wrdSelection.GoToNext(Word.WdGoToItem.wdGoToField); wrdSelection.NextField(); //oWordDoc.MailMerge. //oWordDoc.MailMerge.DataSource.FirstRecord.Equals(num.Number.ToString()); //"INSERT MISSING CODE HERE TO FILL THE DOCX FILE"(num.Number.ToString()); }