- 使用C#在PowerPoint演示文稿中添加VBA宏
- 从PowerPoint演示文稿中提取VBA宏
- 从PowerPoint演示文稿中删除VBA宏
>>你可以点击这里下载Aspose.Slides 最新版测试体验。
整合所有格式API处理控件Aspose.Total永久授权火热促销中,联系客服立马1分钟了解全部!
使用C#在PowerPoint演示文稿中添加VBA宏
以下是使用C#在PowerPoint演示文稿中添加VBA宏的步骤。
- 首先,创建Presentation 类的实例 以加载PowerPoint演示文稿。
- 将一个新的VbaProject分配给 Presentation.VbaProject 属性。
- 使用Presentation.VbaProject.Modules.AddEmptyModule(string)方法添加一个空的VBA模块。
- 将添加的模块的引用获取到IVbaModule对象中。
- 使用IVbaModule.SourceCode属性将源代码添加到VBA模块。
- 创建对Microsoft Office的引用,并将其添加到Presentation.VbaProject.References集合。
- 最后,使用Presentation.Save(string,SaveFormat.Pptm)方法保存演示文稿文件。
下面的代码示例演示如何使用C#在PowerPoint演示文稿中添加VBA宏。
// Load presentationusing (Presentation presentation = new Presentation("presentation.pptm")){ // Create new VBA Project presentation.VbaProject = new VbaProject(); // Add empty module to the VBA project IVbaModule module = presentation.VbaProject.Modules.AddEmptyModule("Module"); // Set module's source code module.SourceCode = @"Sub Test(oShape As Shape) MsgBox ""Test"" End Sub"; // Create reference toVbaReferenceOleTypeLib stdoleReference = new VbaReferenceOleTypeLib("stdole", "*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\Windows\system32\stdole2.tlb#OLE Automation"); // Create reference to Office VbaReferenceOleTypeLib officeReference = new VbaReferenceOleTypeLib("Office", "*\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL#Microsoft Office 14.0 Object Library"); // Add references to the VBA project presentation.VbaProject.References.Add(stdoleReference); presentation.VbaProject.References.Add(officeReference); // Save Presentation presentation.Save("AddVBAMacros.pptm", SaveFormat.Pptm);}
使用C#从PowerPoint中提取VBA宏
Aspose.Slides for .NET还允许您从PowerPoint演示文稿中的VBA项目中提取VBA模块。以下是执行此操作的步骤。
- 首先,使用Presentation 类加载启用宏的PowerPoint演示 文稿。
- 检查 Presentation.VbaProject 属性是否不为null。
- 循环每IVbaModule在Presentation.VbaProject.Modules集合。
- 最后,使用IVbaModule.SourceCode属性提取源代码。
下面的代码示例演示如何使用C#提取PowerPoint VBA宏。
// Load presentationusing (Presentation pres = new Presentation("presentation.pptm")){if (pres.VbaProject != null) // check if Presentation contains VBA Project{foreach (IVbaModule module in pres.VbaProject.Modules){Console.WriteLine(module.Name);Console.WriteLine(module.SourceCode);}}}
删除PowerPoint VBA宏
还可以从PowerPoint演示文稿中删除特定的VBA宏。为此,您将通过VBA项目中的索引访问和删除VBA模块。以下是执行此操作的步骤。
- 首先,使用Presentation 类加载启用宏的PowerPoint演示 文稿。
- 使用Presentation.VbaProject.Modules.Remove(Presentation.VbaProject.Modules [0])方法通过索引删除VBA模块。
- 最后,使用Presentation.Save(string,SaveFormat.Pptm)方法保存更新的演示文稿。
下面的代码示例演示如何删除PowerPoint VBA宏。
// Load presentationusing (Presentation presentation = new Presentation("Presentation.pptm")){ // Remove the VBA module presentation.VbaProject.Modules.Remove(presentation.VbaProject.Modules[0]); // Save presentation presentation.Save("RemovedVBAMacros.pptm", SaveFormat.Pptm);}
如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!