This project has moved. For the latest updates, please go here.
/*
   create macro and connect with button action in PowerPoint
   taken from Example03.cs
*/
private void Example03Main()
{
   PowerPoint.Application powerApplication = null;
   try
   {
       // start powerpoint 
       powerApplication = new PowerPoint.Application();
  
       // 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 VBE.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 string fileExtension = GetDefaultExtension(powerApplication); string documentFile =
string.Format("{0}\\Example03{1}", Application.StartupPath, fileExtension);
presentation.SaveAs(documentFile,
PpSaveAsFileType.ppSaveAsDefault,
MsoTriState.msoTrue); FinishDialog fDialog = new FinishDialog("Presentation saved.", documentFile); fDialog.ShowDialog(this); } catch (Exception throwedException) { // not trusted string message = string.Format(
"An error is occured.{0}ExceptionTrace:{0}", Environment.NewLine); Exception exception = throwedException; while (null != exception) { message += string.Format("{0}{1}",
exception.Message, Environment.NewLine); exception = exception.InnerException; } MessageBox.Show(message); } finally { // close power point and dispose reference if (powerApplication != null) { powerApplication.Quit(); powerApplication.Dispose(); powerApplication = null; } } } /// <summary> /// returns the valid file extension for the instance. for example ".ppt" or ".pptx" /// </summary> /// <param name="application">the instance</param> /// <returns>the extension</returns> private static string GetDefaultExtension(PowerPoint.Application application) { double Version =
Convert.ToDouble(application.Version, CultureInfo.InvariantCulture); if (Version >= 12.00) return ".pptx"; else return ".ppt"; }

Last edited Jun 13, 2012 at 12:16 PM by SebastianDotNet, version 11

Comments

No comments yet.