Arbeiten mit VBA-Makros

Visual Basic for Applications (VBA) für Microsoft Word ist eine einfache, aber leistungsstarke Programmiersprache, mit der die Funktionalität erweitert werden kann. Aspose.Words API bietet drei Klassen, um Zugriff auf den Quellcode des VBA-Projekts zu erhalten:

– Die VBAProject-Klasse bietet Zugriff auf VBA-Projektinformationen – Die VBAModuleCollection-Klasse gibt die Sammlung von VBA-Projektmodulen zurück

  • Die VbaModule-Klasse bietet Zugriff auf das VBA-Projektmodul – Die VbaModuleType-Enumeration definiert die Typen eines Modells in einem VBA-Projekt. Das Modul kann ein prozedurales Modul, ein Dokumentmodul, ein Klassenmodul oder ein Designermodul sein

Erstellen Sie ein VBA-Projekt

Aspose.Words API stellt die VbaProject-Eigenschaft bereit, um VbaProject im Dokument abzurufen oder festzulegen.

Das folgende Codebeispiel zeigt, wie ein VBA-Projekt und ein VBA-Modul zusammen mit grundlegenden Eigenschaften, z. B. Name und Typ, erstellt werden:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Create a new VBA project.
VbaProject project = new VbaProject();
project.Name = "AsposeProject";
doc.VbaProject = project;
// Create a new module and specify a macro source code.
VbaModule module = new VbaModule();
module.Name = "AsposeModule";
module.Type = VbaModuleType.ProceduralModule;
module.SourceCode = "New source code";
// Add module to the VBA project.
doc.VbaProject.Modules.Add(module);
doc.Save(dataDir + "VbaProject_out.docm");

Lesen Sie Makros

Aspose.Words bietet Benutzern außerdem die Möglichkeit, VBA-Makros zu lesen.

Das folgende Codebeispiel zeigt, wie VBA-Makros aus dem Dokument gelesen werden:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.dot");
if (doc.VbaProject != null)
{
foreach (VbaModule module in doc.VbaProject.Modules)
{
Console.WriteLine(module.SourceCode);
}
}

Schreiben oder ändern Sie Makros

Mit Aspose.Words können Benutzer VBA-Makros ändern.

Das folgende Codebeispiel zeigt, wie Sie VBA-Makros mithilfe der SourceCode-Eigenschaft ändern:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "test.docm");
VbaProject project = doc.VbaProject;
const string newSourceCode = "Test change source code";
// Choose a module, and set a new source code.
project.Modules[0].SourceCode = newSourceCode;

VBA-Projekt klonen

Mit Aspose.Words ist es auch möglich, VBA-Projekte zu klonen.

Das folgende Codebeispiel zeigt, wie Sie das VBA-Projekt mithilfe der Clone-Eigenschaft klonen, wodurch eine Kopie des vorhandenen Projekts erstellt wird:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "VbaProject_source.docm");
VbaProject project = doc.VbaProject;
Document destDoc = new Document();
// Clone the whole project.
destDoc.VbaProject = doc.VbaProject.Clone();
destDoc.Save(dataDir + "output.docm");

VBA-Modul klonen

Bei Bedarf können Sie auch VBA-Module klonen.

Das folgende Codebeispiel zeigt, wie Sie das VBA-Modul mithilfe der Clone-Eigenschaft klonen, wodurch eine Kopie des vorhandenen Projekts erstellt wird:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "VbaProject_source.docm");
VbaProject project = doc.VbaProject;
Document destDoc = new Document();
destDoc.VbaProject = new VbaProject();
// Clone a single module.
VbaModule copyModule = doc.VbaProject.Modules["Module1"].Clone();
destDoc.VbaProject.Modules.Add(copyModule);
destDoc.Save(dataDir + "output.docm");

Arbeiten Sie mit den VBA-Projektreferenzen

Aspose.Words API stellt die VbaReferenceCollection-Klasse für die Arbeit mit VBA-Projektreferenzen bereit, die eine Sammlung von VBA-Projektreferenzen darstellen.

Das folgende Codebeispiel zeigt, wie einige Referenzen aus der Referenzsammlung eines VBA-Projekts entfernt werden:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "VbaProject.docm");
// Find and remove the reference with some LibId path.
const string brokenPath = "brokenPath.dll";
VbaReferenceCollection references = doc.VbaProject.References;
for (int i = references.Count - 1; i >= 0; i--)
{
VbaReference reference = doc.VbaProject.References.ElementAt(i);
string path = GetLibIdPath(reference);
if (path == brokenPath)
references.RemoveAt(i);
}
doc.Save(dataDir + "NoBrokenRef.docm");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
/// <summary>
/// Returns string representing LibId path of a specified reference.
/// </summary>
private static string GetLibIdPath(VbaReference reference)
{
switch (reference.Type)
{
case VbaReferenceType.Registered:
case VbaReferenceType.Original:
case VbaReferenceType.Control:
return GetLibIdReferencePath(reference.LibId);
case VbaReferenceType.Project:
return GetLibIdProjectPath(reference.LibId);
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Returns path from a specified identifier of an Automation type library.
/// </summary>
/// <remarks>
/// Please see details for the syntax at [MS-OVBA], 2.1.1.8 LibidReference.
/// </remarks>
private static string GetLibIdReferencePath(string libIdReference)
{
if (libIdReference != null)
{
string[] refParts = libIdReference.Split('#');
if (refParts.Length > 3)
return refParts[3];
}
return "";
}
/// <summary>
/// Returns path from a specified identifier of an Automation type library.
/// </summary>
/// <remarks>
/// Please see details for the syntax at [MS-OVBA], 2.1.1.12 ProjectReference.
/// </remarks>
private static string GetLibIdProjectPath(string libIdProject)
{
return (libIdProject != null) ? libIdProject.Substring(3) : "";
}