private void awardHeadingMasterCombo_SelectedIndexChanged(object sender, EventArgs e) { int comboIndex = awardHeadingMasterCombo.SelectedIndex; mGenInfo.awardHeadingLayoutIndex = comboIndex + 1; PowerPoint.CustomLayout selectedLayout = mPresentation.SlideMaster.CustomLayouts[comboIndex + 1]; mPresentation.Slides.AddSlide(mPresentation.Slides.Count + 1, selectedLayout); string workingDirectory = Directory.GetCurrentDirectory(); awardHeadingTextBoxCombo.Items.Clear(); // Load the placeholder combo boxes and such PowerPoint.Shapes shapes = selectedLayout.Shapes; PowerPoint.Placeholders placeholders = shapes.Placeholders; foreach (PowerPoint.Shape placeholder in placeholders) { awardHeadingTextBoxCombo.Items.Add(placeholder.TextFrame.TextRange.Text); } if (File.Exists(workingDirectory + "/AwardHeadingRender.png")) { if (headingPictureBox.Image != null) { headingPictureBox.Image.Dispose(); } File.Delete(workingDirectory + "/AwardHeadingRender.png"); } mPresentation.Slides[mPresentation.Slides.Count].Export(workingDirectory + "/AwardHeadingRender.png", "png", headingPictureBox.Width, headingPictureBox.Height); headingPictureBox.Image = Image.FromFile(workingDirectory + "/AwardHeadingRender.png"); }
public static void generate(GenInfo genInfo) { Powerpoint.Application powerApp = new Powerpoint.Application(); Powerpoint.Presentation awardsPresentation = powerApp.Presentations.Open(genInfo.awardPowerpointTemplateFile, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse); int startingNumberOfSlides = awardsPresentation.Slides.Count; Excel.Application excelApp = new Excel.Application(); Excel.Workbook awardsWorkbook = excelApp.Workbooks.Open(genInfo.awardExcelFile, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "/t", false, false, 0, true, 1, 0); Excel.Worksheet awardsWorksheet = awardsWorkbook.Worksheets[1]; int lastUsedRow = awardsWorksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row; for (int rowIndex = genInfo.startingRow; rowIndex <= lastUsedRow; rowIndex++) { if (getValueOfCell(ref awardsWorksheet, genInfo.studentCodeColumn, rowIndex) == "") { continue; } Powerpoint.Slide currentSlide = awardsPresentation.Slides.AddSlide(awardsPresentation.Slides.Count + 1, awardsPresentation.SlideMaster.CustomLayouts[genInfo.awardLayoutIndex]); Powerpoint.Shapes slideShapes = currentSlide.Shapes; Powerpoint.Placeholders placeholders = slideShapes.Placeholders; int placeholderIndex = 1; foreach (Powerpoint.Shape shape in placeholders) { // Set Student Name if (placeholderIndex == genInfo.awardStudentNamePlaceholderIndex) { shape.TextFrame.TextRange.Text = getValueOfCell(ref awardsWorksheet, genInfo.studentNameColumn, rowIndex); } // Set Student Form if (placeholderIndex == genInfo.awardStudentFormPlaceholderIndex) { Powerpoint.TextFrame frame = shape.TextFrame; Powerpoint.TextRange range = frame.TextRange; range.Text = getValueOfCell(ref awardsWorksheet, genInfo.studentFormColumn, rowIndex); } // Set award if (placeholderIndex == genInfo.awardAwardTitlePlaceholderIndex) { shape.TextFrame.TextRange.Text = getValueOfCell(ref awardsWorksheet, genInfo.studentAwardColumn, rowIndex); } // Set picture if (placeholderIndex == genInfo.awardStudentPicturePlaceholderIndex) { string studentCode = getValueOfCell(ref awardsWorksheet, genInfo.studentCodeColumn, rowIndex); string pictureFile = findFileInFolder(genInfo.picturesFolder, studentCode + genInfo.pictureFileExtension); if (pictureFile != "") { shape.Fill.UserPicture(pictureFile); } else { Console.Out.WriteLine("Picture for " + studentCode + " not found"); } } placeholderIndex++; } } for (int i = 0; i < startingNumberOfSlides; i++) { awardsPresentation.Slides[1].Delete(); } awardsPresentation.SaveAs(Path.GetDirectoryName(genInfo.awardPowerpointTemplateFile) + "/Output.pptx"); }