vba 取消保护 Excel 工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15225516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unprotect excel sheet
提问by user2002774
Following is my C# code,where i`m trying to unlock protected sheet by passing password,but it still not unlocking the sheet, I need to add some macro to the sheet .
以下是我的 C# 代码,我试图通过传递密码来解锁受保护的工作表,但它仍然没有解锁工作表,我需要向工作表添加一些宏。
Code:
代码:
const string excelFile = @"c:\temp\VBA\test.xlsm";
var excelApplication = new ExcelInterop.Application { Visible = true };
var targetExcelFile = excelApplication.Workbooks.Open(FileName:excelFile,Password:"12asQOl");
I can`t use "sendKeys" here,please help me on this.
我不能在这里使用“sendKeys”,请帮助我。
Thanks
谢谢
回答by Anderson Pimentel
One piece of advice when working with VBA: start recording a macro of what you're trying to do then see the generated code.
使用 VBA 时的一条建议:开始记录您要执行的操作的宏,然后查看生成的代码。
Looks like you're trying to open a protected file, not unprotect a sheet.
看起来您正在尝试打开受保护的文件,而不是取消保护工作表。
I've found some old code of something like this (it's VB):
我发现了一些类似这样的旧代码(它是 VB):
Dim WSheet As Worksheet
For Each WSheet In Worksheets
If WSheet.ProtectContents = True Then
WSheet.Unprotect Password:=MyPassword
Else
WSheet.Protect Password:=MyPassword
End If
Next WSheet
回答by Kokulan Eswaranathan
In your code you are passing the document password. You are not un-protecting the workbook.
在您的代码中,您正在传递文档密码。您并没有取消保护工作簿。
Use the following code to un-protect the workbook:
使用以下代码取消保护工作簿:
targetExcelFile.Unprotect(password);
Let me know if you have any issues.
如果您有任何问题,请告诉我。