Presentation via VBA
The Aspose.Slides.Vba namespace contains classes and interfaces for working with macros and VBA code.
Note
When you convert a presentation containing macros to a different file format (PDF, HTML, etc.), Aspose.Slides ignores all macros (macros are not carried into the resulting file).
When you add macros to a presentation or resave a presentation containing macros, Aspose.Slides simply writes the bytes for the macros.
Aspose.Slides never runs the macros in a presentation.
Add VBA Macros
Aspose.Slides provides the VbaProject class to allow you to create VBA projects (and project references) and edit existing modules. You can use the IVbaProject interface to manage VBA embedded in a presentation.
- Create an instance of the Presentation class.
- Use the VbaProject constructor to add a new VBA project.
- Add a module to the VbaProject.
- Set the module source code.
- Add references to
. - Add references to Microsoft Office.
- Associate the references with the VBA project.
- Save the presentation.
This C++ code shows you how to add a VBA macro from scratch to a presentation:
// The path to the documents directory.
const String outPath = u"../out/AddVBAMacros_out.pptm";
// Creates an instance of the presentation class
SharedPtr<Presentation> presentation = MakeObject<Presentation>();
// Creates a new VBA Project
presentation->set_VbaProject(MakeObject<VbaProject>());
// Adds an empty module to the VBA project
SharedPtr<IVbaModule> module = presentation->get_VbaProject()->get_Modules()->AddEmptyModule(u"Module");
// Sets the module source code
module->set_SourceCode(u"Sub Test(oShape As Shape) MsgBox \"Test\" End Sub");
// Creates a 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");
// Creates a 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");
// Adds references to the VBA project
presentation->get_VbaProject()->get_References()->Add(stdoleReference);
presentation->get_VbaProject()->get_References()->Add(officeReference);
// Saves the Presentation
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptm);
Remove VBA Macros
Using the VbaProject property under the Presentation class, you can remove a VBA macro.
- Create an instance of the Presentation class and load the presentation containing the macro.
- Access the Macro module and remove it.
- Save the modified presentation.
This C++ code shows you how to remove a VBA macro:
// The path to the documents directory.
const String outPath = u"../out/RemoveVBAMacros_out.pptm";
const String templatePath = u"../templates/vba.pptm";
// Loads the presentation containing the macro
SharedPtr<Presentation> presentation = MakeObject<Presentation>(templatePath);
// Accesses the Vba module and removes it
presentation->get_VbaProject()->get_Modules()->Remove(presentation->get_VbaProject()->get_Modules()->idx_get(0));
// Saves the Presentation
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptm);
Extract VBA Macros
- Create an instance of the Presentation class and load the presentation containing the macro.
- Check if the presentation contains a VBA Project.
- Loop through all the modules contained in the VBA Project to view the macros.
This C++ code shows you how to extract VBA macros from a presentation containing macros:
// The path to the documents directory.
const String templatePath = u"../templates/VBA.pptm";
// Loads the presentation containing the macro
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
if (pres->get_VbaProject() != NULL) // Checks whether the Presentation contains a 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());
}
}