管理 Visio 宏启用 diagram 的 VBA 代码。

添加 VBA 模块

下面的示例代码添加了一个新的 VBA 模块和宏代码,并以 VSDM 格式保存输出。一次,你将在Microsoft Visio中打开输出VSDM文件,点击开发人员 > Visual Basic菜单命令,您将看到一个名为“TestModule”的模块,在其中,您将看到以下宏代码。

 Sub ShowMessage()

    MsgBox "Welcome to Aspose!"

End Sub

下面是使用 VBA 模块和宏代码生成输出 VSDM 文件的示例代码。

// ExStart:ApplyThemeToNewShape
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Shapes();
// Load a diagram
Diagram diagram = new Diagram(dataDir + "1.vsdm");
//Add module
int index = diagram.VbaProject.Modules.Add(VbaModuleType.Procedural, "TestModule");
//Get module
Aspose.Diagram.Vba.VbaModule module = diagram.VbaProject.Modules[index];
//Set module
module.Codes = "Attribute VB_Name = \"module2\"\r\n Sub Button1_Click()\r\n\r\n MsgBox \"Welcome to Aspose!\"\r\n\r\nEnd Sub\r\n";
diagram.Save(dataDir + "1out.vsdm", SaveFileFormat.VSDM);

修改 VBA 或宏

以下示例代码加载源文件 Visio,其中包含以下 VBA 或宏代码

 Sub Button1_Click()

    MsgBox "This is test message."

End Sub

Aspose.Diagram示例代码执行后,VBA或Macro代码会修改成这样

 Sub Button1_Click()

    MsgBox "This is Aspose.Diagram message."

End Sub

您可以下载源文件 Visio输出 Visio 文件从给定的链接。

// ExStart:ApplyThemeToNewShape
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Shapes();
// Load a diagram
Diagram diagram = new Diagram(dataDir + "1.vsdm");
//Get module
Aspose.Diagram.Vba.VbaModule module = diagram.VbaProject.Modules[2];
//Set module
module.Codes = "Attribute VB_Name = \"module2\"\r\n Sub Button1_Click()\r\n\r\n MsgBox \"This is Aspose.Diagram message.\"\r\n\r\nEnd Sub\r\n";
diagram.Save(dataDir + "1out.vsdm", SaveFileFormat.VSDM);

推进主题