Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing word interop create multiple documents from a template for print preview
    text
    copied!<p>I have an app where the user selects from a list all the students they want to print an award document for.</p> <p>I have a template .doc file that contains 3 text boxes that I populate from code. I can populate the file and show it in the print preview for a single student, but based on how many students are selected I want to create a large document with many pages I can see in print preview before printing and print all at once. </p> <p>The following is my attempt to turn my working code for a single word document being created from the template to show in print preview. Any ideas?</p> <pre><code> public void AddStudentToDocument(IEnumerable&lt;StudentToPrint&gt; studentsToPrint ) { _Application oWordApp = new Application(); string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string specificFolder = Path.Combine(folder, "FoothillsAcademy"); string fileLocation = Path.Combine(specificFolder, "CertTemplate.doc"); if (File.Exists(fileLocation)) { var oWordDoc = oWordApp.Documents.Open(fileLocation); oWordDoc.Activate(); oWordApp.Selection.TypeParagraph(); foreach (var studentToPrint in studentsToPrint) { _Document oDoc = oWordApp.Documents.Add(); Selection oSelection = oWordApp.Selection; string docText = oWordDoc.Content.Text; if (docText != null) { int boxNumber = 1; foreach (Shape shape in oWordApp.ActiveDocument.Shapes) { if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox) { if (boxNumber == 1) { shape.TextFrame.TextRange.Text = studentToPrint.StudentName; } if (boxNumber == 2) { shape.TextFrame.TextRange.Text = studentToPrint.Rank; } if (boxNumber == 3) { shape.TextFrame.TextRange.Text = studentToPrint.DateAcheved; } boxNumber++; } } _Document oCurrentDocument = oWordApp.Documents.Add(oWordDoc); copyPageSetup(oCurrentDocument.PageSetup, oDoc.Sections.Last.PageSetup); oCurrentDocument.Range().Copy(); oSelection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting); //if (!Object.ReferenceEquals(oWordDoc.Content, oWordDoc.Last())) oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage); } oWordApp.Visible = true; oWordApp.ShowStartupDialog = true; oWordApp.ActiveDocument.PrintPreview(); } } } private void copyPageSetup(PageSetup source, PageSetup target) { target.PaperSize = source.PaperSize; if (!source.Orientation.Equals(target.Orientation)) target.TogglePortrait(); target.TopMargin = source.TopMargin; target.BottomMargin = source.BottomMargin; target.RightMargin = source.RightMargin; target.LeftMargin = source.LeftMargin; target.FooterDistance = source.FooterDistance; target.HeaderDistance = source.HeaderDistance; target.LayoutMode = source.LayoutMode; } public class StudentToPrint { public string StudentName { get; set; } public string Rank { get; set; } public string DateAcheved { get; set; } } </code></pre> <p>Currently I am testing this with a collection of StudentsToPrint added below. Based on the data below I would expect to see 3 certificates personalized for each of the 3 students. Each certificate would be on its own page.</p> <pre><code>List&lt;StudentToPrint&gt; listOfStudents = new List&lt;StudentToPrint&gt; { new StudentToPrint { DateAcheved = DateTime.Now.ToShortDateString(), Rank = "5th Degree", StudentName = "Scott LaFoy" }, new StudentToPrint { DateAcheved = DateTime.Now.ToShortDateString(), Rank = "3rd Degree", StudentName = "John Doe" }, new StudentToPrint { DateAcheved = DateTime.Now.ToShortDateString(), Rank = "2nd Degree", StudentName = "Jane Doe" } }; </code></pre> <p>The template is a word doc that has 3 text boxes. Using the text box lets me set font and position different for each of them as well as making the background transparent so the template background shows through. I am sure there is another way to do this as well but there is not much on this topic around. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload