Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing word interop create multiple documents from a template for print preview
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI know its possible and shouldn't be this hard. Does anyone know of a wrapper that that might be a little easier to work with?
      singulars
    2. COI will explain you what is the problem here: you are coming from a completely wrong approach. I try to fix small bits but wrong results keep appearing (because your whole approach is wrong). I don't to redo your whole code (this is not what SO is about) and, as a reward, I have to work under not-too-sensible conditions (correcting problems which shouldn't be there, because are provoked by a basic wrong implementation). I guess that I am a horrible answerer for this kind of questions, but I am always intending to help. Now I will write down a summary of the problems, to see what I mean...
      singulars
    3. CO... your code does not clearly separate the two sets of documents (template and new ones being generated). My answer provided a first solution on this front, but was just fictitiously good, as far as your structure is bad and needs to be redone. You have to create two word apps/docs (as suggested): first one will be a blank file (var oWordDoc = oWordApp.Documents.Add();... it has to be saved as a different name oWordDoc.SaveAs2(specificFolder + @"\output.docx");) and second one will open the template (_Document oDoc = oWordApp2.Documents.Open(fileLocation);)
      singulars
 

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