ExcelワークブックのVBAプロジェクトにパスワードを設定する
Contents
[
Hide
]
C#でExcelワークブックのVBAプロジェクトをパスワードで保護
Aspose.Cellsを使用して、ワークブックのVBA(Visual Basic for Applications)プロジェクトをパスワードで保護できます。VbaProject.Protect()メソッドを使用します。
サンプルコード
以下のサンプルコードは、サンプルExcelファイルを読み込み、そのVBAプロジェクトにパスワードを設定して保護し、最終的に出力Excelファイルとして保存します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |