密码保护 Excel 工作簿的 VBA 项目

在 C# 中为 Excel 工作簿密码保护 VBA 项目

您可以使用 Aspose.Cells 的 VbaProject.Protect() 方法为工作簿的 VBA (Visual Basic for Applications)项目设置密码保护。

示例代码

以下示例代码加载 示例 Excel 文件,访问其 VBA 项目并使用密码保护它。最后将其保存为 输出 Excel 文件

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//Load your source Excel file.
Workbook wb = new Workbook(dataDir + "samplePasswordProtectVBAProject.xlsm");
//Access the VBA project of the workbook.
Aspose.Cells.Vba.VbaProject vbaProject = wb.VbaProject;
//Lock the VBA project for viewing with password.
vbaProject.Protect(true, "11");
//Save the output Excel file
wb.Save(dataDir + "outputPasswordProtectVBAProject.xlsm");