public void RunExample() { bool isFailed = false; string documentFile = null; PowerPoint.Application powerApplication = null; try { // start powerpoint powerApplication = COMObject.Create <PowerPoint.Application>(); // create a utils instance, no need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(powerApplication); // add a new presentation with one new slide PowerPoint.Presentation presentation = powerApplication.Presentations.Add(MsoTriState.msoTrue); PowerPoint.Slide slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank); // add new module and insert macro. the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = presentation.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; string macro = string.Format("Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "MsgBox \"Thanks for click!\""); module.InsertLines(1, macro); // add button and connect with macro PowerPoint.Shape button = slide.Shapes.AddShape(MsoAutoShapeType.msoShapeActionButtonForwardorNext, 100, 100, 200, 200); button.ActionSettings[PpMouseActivation.ppMouseClick].AnimateAction = MsoTriState.msoTrue; button.ActionSettings[PpMouseActivation.ppMouseClick].Action = PpActionType.ppActionRunMacro; button.ActionSettings[PpMouseActivation.ppMouseClick].Run = "NetOfficeTestMacro"; // save the document documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example03", DocumentFormat.Macros); presentation.SaveAs(documentFile); } catch (System.Runtime.InteropServices.COMException throwedException) { isFailed = true; HostApplication.ShowErrorDialog("VBA Error", throwedException); } finally { // close power point and dispose reference if (powerApplication != null) { powerApplication.Quit(); powerApplication.Dispose(); } if ((null != documentFile) && (!isFailed)) { HostApplication.ShowFinishDialog(null, documentFile); } } }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); // save the document string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example05", DocumentFormat.Macros); newDocument.SaveAs(documentFile); if (utils.ApplicationIs2007OrHigher) { newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); } else { newDocument.SaveAs(documentFile); } //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show end dialog HostApplication.ShowFinishDialog(null, documentFile); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); string fileExtension = GetFileExtension(wordApplication); string documentFile = string.Format("{0}\\Example05{1}", _hostApplication.RootDirectory, fileExtension); double wordVersion = Convert.ToDouble(wordApplication.Version, CultureInfo.InvariantCulture); if (wordVersion >= 12.0) { newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); } else { newDocument.SaveAs(documentFile); } //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) _hostApplication.ShowFinishDialog(null, documentFile); }