與VBA巨集一起工作
Visual Basic for Applications (VBA) Aspose.Words for Microsoft Word是一個簡單但強大的編程語言,可以用來擴展功能。API提供三個類別來獲取對VBA項目源代碼的訪問:
“- VBAProject 類別提供對 VBA 專案資訊的存取” “- VBAModuleCollection 類別會傳回 VBA 專案模組的集合” “- VbaModule類別提供存取VBA專案模組的服務”
- VbaModuleType 枚舉定義了 VBA 專案中模型的類別。 該模組可以是程序化模組、文件模組、類別模組或設計師模組。
建立 Visual Basic 專案
Aspose.Words API 在該文件中為 VbaProject 提供 VbaProject 屬性以獲取或設定。
以下程式碼示例說明如何建立 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,也能夠複製 Visual Basic for Applications 專案。
接下來的程式碼範例示範了如何透過 Clone 屬性複製 Visual Basic for Applications (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) : ""; | |
} |