Worksheet.Unprotect - Office Interop - 2003 年和 2007 年之间的差异

发布于 2024-08-23 18:39:47 字数 1844 浏览 5 评论 0原文

我有一个 .NET winforms 应用程序,可以自动执行 Excel 并检查工作表密码。要求是能够检测到 1)保护已关闭 2)密码被删除(受保护但没有密码) 3) 密码与数据库中的正确密码匹配

为了满足第二个要求,程序使用空字符串调用 Worksheet.Unprotect 命令,捕获错误。如果出现预期错误,则进行第三次检查。如果没有错误,则取消保护无需密码即可工作==>密码已删除。

下面的代码示例具有这些检查。

该应用程序可以在 Office 2003 中很好地完成此操作。此后,我将我的开发计算机更新到 Office 2007,但它不再像以前那样工作。当我调用Worksheet.Unprotect时,Excel提示输入密码!

我需要知道如何在新版本的 Excel 中完成此操作,或者是否有办法引用旧的 PIA。不管怎样,如果我设置对 Excel 11 的引用,它会在 GAC 中替换为 12 的 PIA。

'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

I have a .NET winforms app that automates Excel and checks for a worksheet password. 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

To meet the second requirement the program calls the Worksheet.Unprotect command with a null string, capturing the error. 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. When I call the Worksheet.Unprotect, Excel prompts for the password!

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. No matter what if I set a reference to Excel 11 it is replaced with the PIA for 12 in the GAC.

'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2024-08-30 18:39:47

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

Use the Worksheet.ProtectionMode property to determine if the worksheet is protected (instead of the try..catch attempt), and try Unprotect("") to attempt unprotecting worksheets with blank passwords.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文