Διαχείριση έργων VBA σε παρουσιάσεις χρησιμοποιώντας C++

Εισαγωγή

The Aspose.Slides.Vba namespace contains classes and interfaces for working with macros and VBA code.

Προσθήκη VBA Μακροεντολών

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.

  1. Create an instance of the Presentation class.
  2. Use the VbaProject constructor to add a new VBA project.
  3. Add a module to the VbaProject.
  4. Set the module source code.
  5. Add references to .
  6. Add references to Microsoft Office.
  7. Associate the references with the VBA project.
  8. Save the presentation.

This C++ code shows you how to add a VBA macro from scratch to a presentation:

// Η διαδρομή προς το φάκελο εγγράφων.
const String outPath = u"../out/AddVBAMacros_out.pptm";

// Δημιουργεί μια παρουσία της κλάσης Presentation
SharedPtr<Presentation> presentation = MakeObject<Presentation>();
// Δημιουργεί ένα νέο έργο VBA
presentation->set_VbaProject(MakeObject<VbaProject>());

// Προσθέτει ένα κενό module στο έργο VBA
SharedPtr<IVbaModule> module = presentation->get_VbaProject()->get_Modules()->AddEmptyModule(u"Module");

// Ορίζει τον κώδικα πηγής του module
module->set_SourceCode(u"Sub Test(oShape As Shape) MsgBox \"Test\" End Sub");

// Δημιουργεί μια αναφορά στο <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");

// Δημιουργεί μια αναφορά στο 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");

// Προσθέτει αναφορές στο έργο VBA
presentation->get_VbaProject()->get_References()->Add(stdoleReference);
presentation->get_VbaProject()->get_References()->Add(officeReference);

// Αποθηκεύει την παρουσίαση
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptm);

Αφαίρεση VBA Μακροεντολών

Using the VbaProject property under the Presentation class, you can remove a VBA macro.

  1. Create an instance of the Presentation class and load the presentation containing the macro.
  2. Access the Macro module and remove it.
  3. Save the modified presentation.

This C++ code shows you how to remove a VBA macro:

// Η διαδρομή προς το φάκελο εγγράφων.
const String outPath = u"../out/RemoveVBAMacros_out.pptm";
const String templatePath = u"../templates/vba.pptm";

// Φορτώνει την παρουσίαση που περιέχει τη μακροεντολή
SharedPtr<Presentation> presentation = MakeObject<Presentation>(templatePath);

// Πρόσβαση στο module Vba και αφαίρεσή του 
presentation->get_VbaProject()->get_Modules()->Remove(presentation->get_VbaProject()->get_Modules()->idx_get(0));

// Αποθηκεύει την παρουσίαση
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptm);

Εξαγωγή VBA Μακροεντολών

  1. Create an instance of the Presentation class and load the presentation containing the macro.
  2. Check if the presentation contains a VBA Project.
  3. 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:


	// Η διαδρομή προς το φάκελο εγγράφων.
	const String templatePath = u"../templates/VBA.pptm";

	// Φορτώνει την παρουσίαση που περιέχει τη μακροεντολή
	SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);


	if (pres->get_VbaProject() != NULL) // Ελέγχει αν η παρουσίαση περιέχει έργο VBA
	{
		
		//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());
		}
	}

Έλεγχος εάν ένα VBA Project είναι προστατευμένο με κωδικό

Using the IVbaProject::get_IsPasswordProtected property, you can determine whether a project’s properties are password-protected.

  1. Create an instance of the Presentation class and load a presentation that contains a macro.
  2. Check whether the presentation contains a VBA project.
  3. Check whether the VBA project is password-protected to view its properties.
auto presentation = MakeObject<Presentation>(u"VBA.pptm");
    
if (presentation->get_VbaProject() != nullptr) // Ελέγχει αν η παρουσίαση περιέχει έργο VBA.
{
    if (presentation->get_VbaProject()->get_IsPasswordProtected())
    {
        Console::WriteLine(u"The VBA Project '{0}' is protected by password to view project properties.", presentation->get_VbaProject()->get_Name());
    }
}
    
presentation->Dispose();

Συχνές ερωτήσεις

What happens to macros if I save the presentation as PPTX?

Macros will be removed because PPTX does not support VBA. To keep macros, choose PPTM, PPSM, or POTM.

Can Aspose.Slides run macros inside a presentation to, for example, refresh data?

No. The library never executes VBA code; execution is only possible inside PowerPoint with the appropriate security settings.

Is working with ActiveX controls linked to VBA code supported?

Yes, you can access existing ActiveX controls, modify their properties, and remove them. This is useful when macros interact with ActiveX.