简体   繁体   English

Worksheet.Unprotect-Office Interop-2003和2007之间的区别

[英]Worksheet.Unprotect - Office Interop - Difference between 2003 and 2007

I have a .NET winforms app that automates Excel and checks for a worksheet password. 我有一个.NET winforms应用程序,该应用程序可以自动执行Excel并检查工作表密码。 The requirements are to be able to detect 1) that the protection is turned off 2) that the password is removed (protected but there is no password) 3) that the password matches the correct password from a database 要求是能够检测到1)保护已关闭2)密码已删除(受保护但没有密码)3)密码与数据库中的正确密码匹配

To meet the second requirement the program calls the Worksheet.Unprotect command with a null string, capturing the error. 为了满足第二个要求,程序将使用空字符串调用Worksheet.Unprotect命令,以捕获错误。 If error as expected, the 3rd check is made. 如果预期错误,则进行第三次检查。 If no error, then the Unprotect worked without a password ==> password was removed. 如果没有错误,则取消保护无需密码即可工作==>密码已删除。

The code sample below has these checks. 下面的代码示例具有这些检查。

The application can do this fine with Office 2003. I have since had my dev machine updated to Office 2007 and it no longer works as it did. 该应用程序可以在Office 2003上很好地完成工作。此后,我将开发机更新到了Office 2007,并且不再像以前那样工作了。 When I call the Worksheet.Unprotect, Excel prompts for the password! 当我调用Worksheet.Unprotect时,Excel会提示您输入密码!

I need to know how this should be accomplished in the new version of Excel or if there is a way to reference the old PIA. 我需要知道如何在新版本的Excel中完成此操作,或者是否有引用旧PIA的方法。 No matter what if I set a reference to Excel 11 it is replaced with the PIA for 12 in the GAC. 无论我如何设置对Excel 11的引用,GAC中都将其替换为PIA for 12。

'return true if unprotect of worksheet does not generate an error
    'all other errors will bubble up
    'return false if specific error is "Password is invalid..."
    Try
        'detect unprotected or no password
        If oWorksheet.ProtectContents Then
            'try with no passsword and expect an error
            'if no error then raise exception
            Dim blnRaiseException As Boolean = True
            Try
                'oWorksheet.Unprotect(vbNullString)
                oWorksheet.Unprotect()
            Catch ex As Exception
                blnRaiseException = False
            End Try

            If blnRaiseException Then
                Throw New ExcelSheetNoPasswordException
            End If

            oWorksheet.Unprotect(strPwd)
            'no error so if we get here -- success
            fnCheckWorksheetPwd = True

            'leave as it was -- this may still cause workbook to think it is changed
            oWorksheet.Protect(strPwd)
        Else
            Throw New ExcelSheetNotProtectedException
        End If

    Catch COMex As System.Runtime.InteropServices.COMException
        'handle error code -2146827284 
        If COMex.ErrorCode = -2146827284 Then
            'this is the error we're looking for
        Else
            Throw
        End If
    Catch ex As Exception
        Throw
    End Try

使用Worksheet.ProtectionMode属性确定工作表是否受保护(而不是try..catch尝试),然后尝试Unprotect(“”)尝试用空白密码取消保护工作表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM