检查VBA代码的数字签名是否有效
Contents
[
Hide
]
Aspose.Cells允许您使用Workbook.VbaProject.IsValidSigned属性检查VBA代码的数字签名是否有效。如果签名有效,则返回true,否则返回false。当您更改VBA代码时,VBA代码的数字签名将变为无效。
在C#中检查VBA代码的数字签名是否有效
以下代码演示了使用该属性的用法,使用示例Excel文件进行测试。相同的Excel文件具有有效的签名,但当我们修改其VBA代码并保存工作簿后重新检查时,我们发现其签名已变为无效。
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); | |
Workbook workbook = new Workbook(dataDir + "sampleVBAProjectSigned.xlsm"); | |
// Signature is valid | |
Console.WriteLine("Is VBA Code Project Valid Signed: " + workbook.VbaProject.IsValidSigned); | |
// Modify the VBA Code, save the workbook then reload | |
// VBA Code Signature will now be invalid | |
string code = workbook.VbaProject.Modules[1].Codes; | |
code = code.Replace("Welcome to Aspose", "Welcome to Aspose.Cells"); | |
workbook.VbaProject.Modules[1].Codes = code; | |
// Save | |
workbook.Save(dataDir + "output_out.xlsm"); | |
// Reload | |
workbook = new Workbook(dataDir + "output_out.xlsm"); | |
// Now the signature is invalid | |
Console.WriteLine("Is VBA Code Project Valid Signed: " + workbook.VbaProject.IsValidSigned); |