使用 VBA 宏

Visual Basic for Applications (VBA) for Microsoft Word 是一种简单但功能强大的编程语言,可用于扩展功能。 Aspose.Words API 提供了三个类来访问 VBA 项目源代码:

  • VBAProject 类提供对 VBA 项目信息的访问
  • VBAModuleCollection 类返回 VBA 项目模块的集合
  • VbaModule 类提供对 VBA 项目模块的访问
  • VbaModuleType 枚举定义 VBA 项目中模型的类型。该模块可以是程序模块、文档模块、类模块或设计器模块

创建 VBA 项目

Aspose.Words API 提供 VbaProject 属性来获取或设置文档中的 VbaProject。

以下代码示例演示如何创建 VBA 项目和 VBA 模块以及基本属性(例如名称和类型):

// 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");

读取宏

Aspose.Words 还为用户提供了读取 VBA 宏的能力。

以下代码示例显示如何从文档中读取 VBA 宏:

// 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);
}
}

编写或修改宏

使用 Aspose.Words,用户可以修改 VBA 宏。

以下代码示例显示如何使用 SourceCode 属性修改 VBA 宏:

// 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 项目

使用 Aspose.Words 还可以克隆 VBA 项目。

以下代码示例演示如何使用 Clone 属性克隆 VBA 项目,该属性创建现有项目的副本:

// 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 模块

如果需要,您还可以克隆 VBA 模块。

以下代码示例演示如何使用 Clone 属性克隆 VBA 模块,该属性创建现有项目的副本:

// 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");

使用 VBA 项目参考

Aspose.Words API 提供 VbaReferenceCollection 类来与表示 VBA 项目引用集合的 VBA 项目引用一起使用。

以下代码示例演示如何从 VBA 项目的引用集合中删除一些引用:

// 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) : "";
}