Check whether Excel file is Password protected

前端 未结 2 1159
灰色年华
灰色年华 2020-12-22 00:00

I am trying to open an Excel (xlsm) file via VBA. It may or may not be protected with a (known) password. I am using this code:

On Error Resume Next
Workbook         


        
相关标签:
2条回答
  • 2020-12-22 00:42

    For the record - the reason was indeed the structure protection of the workbook I was opening. I could circumvent the problem by disabling structure protection, and re-protecting in Workbook_Open() (in the protected workbook).

    With structure protection inactive, Workbooks.Open with password does not fail even when there is no password.

    Since I am opening via a VBA method, the VBA code is already trusted, meaning the Workbook_Open method will surely be called.

    0 讨论(0)
  • 2020-12-22 00:45
    1. Use 1004 error handling exclusively for this particular problem, and On Error Resume Next for everything else in the Sub.

    2. Above the statement Workbooks.Open filename, Password:=user_entered_pw also add another line statement without a password argument.

    3. If those (or their combination) don't work, try scouring the .xlsm flat code in Notepad for any hint that a PW protected file differs at that level from one without. Use that info in a pre-opening function.

    0 讨论(0)
提交回复
热议问题