PPT处理控件Aspose.Slides功能演示:使用 Java 在 PowerPoint 中添加、提取和删除 VBA 宏

  • 在 PowerPoint 演示文稿中添加 VBA 宏
  • 从 PowerPoint 演示文稿中提取 VBA 宏
  • 从 PowerPoint 演示文稿中删除 VBA 宏

为了在演示中使用 VBA 宏,我们将使用Aspose.Slides for Java,它是一个功能丰富的 API,可让您从 Java 应用程序中创建或操作 PowerPoint 演示文稿。

>>你可以点击这里下载Aspose.Slides 最新版测试体验。

PPT处理控件Aspose.Slides功能演示:使用 Java 在 PowerPoint 中添加、提取和删除 VBA 宏

使用 Java 在 PowerPoint 演示文稿中添加 VBA 宏

以下是使用 Java 在 PowerPoint 演示文稿中添加 VBA 宏的步骤。

  • 首先,创建Presentation 类的实例 以加载 PowerPoint 演示文稿。
  • 使用 Presentation.setVbaProject() 方法分配一个新的VbaProject。
  • 使用Presentation.getVbaProject().getModules().addEmptyModule(String)方法添加一个空的 VBA 模块。
  • 将添加的模块的引用获取到IVbaModule对象中。
  • 使用IVbaModule.setSourceCode()方法将源代码添加到 VBA 模块。
  • 创建对 Microsoft Office 的引用并将它们添加到Presentation.getVbaProject().getReferences()集合。
  • 最后,使用Presentation.save(String, SaveFormat.Pptm)方法保存演示文件。

以下代码示例展示了如何使用 Java 在 PowerPoint 演示文稿中添加 VBA 宏。

// Instantiate PresentationPresentation pres = new Presentation();try {    // Create new VBA Project    pres.setVbaProject(new VbaProject());     // Add empty module to the VBA project    IVbaModule module = pres.getVbaProject().getModules().addEmptyModule("Module");     // Set module source code    module.setSourceCode("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    pres.getVbaProject().getReferences().add(stdoleReference);    pres.getVbaProject().getReferences().add(officeReference);     pres.save("test.pptm", SaveFormat.Pptm);} finally {    if (pres != null) pres.dispose();}

使用 Java 从 PowerPoint 中提取 VBA 宏

以下是使用 Java 从 PowerPoint 演示文稿中提取 VBA 宏的步骤。

  • 首先,使用Presentation 类加载启用宏的 PowerPoint 演示 文稿。
  • 然后,检查 Presentation.getVbaProject() 是否 返回 null。
  • 使用Presentation.getVbaProject().getModules()循环遍历集合中的每个IVbaModule。
  • 最后,使用IVbaModule.getSourceCode()方法提取源代码。

以下代码示例显示了如何提取 PowerPoint VBA 宏。

// Load PresentationPresentation pres = new Presentation("VBA.pptm");try {    if (pres.getVbaProject() != null) // check if Presentation contains VBA Project    {        // Loop through the VBA modules        for (IVbaModule module : pres.getVbaProject().getModules())        {            System.out.println(module.getName());            System.out.println(module.getSourceCode());        }    }} finally {    if (pres != null) pres.dispose();}

删除 PowerPoint VBA 宏

还可以使用 VBA 项目中的索引从 PowerPoint 中删除特定的 VBA 宏。以下是执行此操作的步骤。

  • 首先,使用Presentation 类加载启用宏的 PowerPoint 演示 文稿。
  • 然后,使用Presentation.getVbaProject().getModules().remove(IVbaModule)方法按索引删除 VBA 模块。
  • 最后,使用Presentation.save(String, SaveFormat.Pptm)方法保存更新的演示文稿。

下面的代码示例展示了如何删除 PowerPoint VBA 宏。

// Load PresentationPresentation pres = new Presentation("VBA.pptm");try {    // Access the Vba module and remove    pres.getVbaProject().getModules().remove(pres.getVbaProject().getModules().get_Item(0));     // Save Presentation    pres.save("test.pptm", SaveFormat.Pptm);} finally {    if (pres != null) pres.dispose();}

如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年7月3日
下一篇 2021年7月3日

相关推荐

发表回复

登录后才能评论