Presentation via VBA
Add VBA Macros
The Presentation class previous VbaProject property has been replaced. Now instead of the raw bytes of the VbaProject property representation of VBA project, the new IVbaProject interface implementation has been added. Use IVbaProject
to manage VBA embedded in a presentation. You can add new project references, edit existing modules and create new ones. Also, you can create a new VBA project using the VbaProject
class which implements the VbaProject
interface. The following example shows how to create a simple VBA project. It contains one module and adds two required references to the libraries.
- Create an instance of the Presentation class.
- Add a new
VbaProject
with thePresentation.VbaProject
property. - Add a module to the VbaProject.
- Set the module source code.
- Add references to
. - Add references to Microsoft Office.
- Associate the references with the
VbaProject
. - Finally, write the PPTX file using the Presentation object.
The implementation of the above steps is demonstrated in the example below.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../out/AddVBAMacros_out.pptm"; | |
// Load the desired the presentation | |
SharedPtr<Presentation> presentation = MakeObject<Presentation>(); | |
// Create new VBA Project | |
presentation->set_VbaProject(MakeObject<VbaProject>()); | |
// Add empty module to the VBA project | |
SharedPtr<IVbaModule> module = presentation->get_VbaProject()->get_Modules()->AddEmptyModule(u"Module"); | |
// Set module source code | |
module->set_SourceCode(u"Sub Test(oShape As Shape) MsgBox \"Test\" End Sub"); | |
// Create reference to <stdole> | |
SharedPtr<VbaReferenceOleTypeLib> stdoleReference = | |
MakeObject<VbaReferenceOleTypeLib>(u"stdole", u"*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation"); | |
// Create reference to Office | |
SharedPtr<VbaReferenceOleTypeLib> officeReference = | |
MakeObject<VbaReferenceOleTypeLib>(u"Office", u"*\\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->get_VbaProject()->get_References()->Add(stdoleReference); | |
presentation->get_VbaProject()->get_References()->Add(officeReference); | |
// Save PPTX to Disk | |
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptm); | |
Remove VBA Macros
The Presentation class now has included the support to remove the VBA macros inside presentation. The following example shows how to access and remove a VBA macro in presentation.
- Create an instance of the Presentation class and load presentation with Macro.
- Access the Macro module and remove that
- Finally, write the PPTX file using the Presentation class object.
The implementation of the above steps is demonstrated in the example below.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../out/RemoveVBAMacros_out.pptm"; | |
const String templatePath = u"../templates/vba.pptm"; | |
// Load the desired the presentation | |
SharedPtr<Presentation> presentation = MakeObject<Presentation>(templatePath); | |
// Access the Vba module and remove | |
presentation->get_VbaProject()->get_Modules()->Remove(presentation->get_VbaProject()->get_Modules()->idx_get(0)); | |
// Save PPTX to Disk | |
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptm); | |
Extract VBA Macros
Aspose.Slides for C++ supports extracting VBA Macros from the slide. In order to extract VBA Macros, please follow the steps below:
- Load a Presentation containing a VBA Macros
- Check if Presentation contains VBA Project
- Loop through all the modules that are contained in the VBA Project
The implementation of the above steps is demonstrated in the example below.
// The path to the documents directory. | |
const String templatePath = u"../templates/VBA.pptm"; | |
// Load the desired the presentation | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
if (pres->get_VbaProject() != NULL) // check if Presentation contains VBA Project | |
{ | |
//for (SharedPtr<IVbaModule> module : pres->get_VbaProject()->get_Modules()) | |
for (int i = 0; i < pres->get_VbaProject()->get_Modules()->get_Count(); i++) | |
{ | |
SharedPtr<IVbaModule> module = pres->get_VbaProject()->get_Modules()->idx_get(i); | |
System::Console::WriteLine(module->get_Name()); | |
System::Console::WriteLine(module->get_SourceCode()); | |
} | |
} |