tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,926
- Office Version
- 365
- 2019
- Platform
- Windows
The problem I have is to open password protected files.
I know of 2 possible passwords, so if either works, then fine, otherwise I want to display a message informing the user that neither passwords are correct.
This is my code:
Is the code OK or does it need tidying?
As far as I am aware, every time you handle an error, you must use the Resume word instead of GoTo. However in my example, I tried:
but it didn't go to the line SomeLine. Instead it went to the next line in the code.
Also does putting:
reset the error handling status?
Thanks
I know of 2 possible passwords, so if either works, then fine, otherwise I want to display a message informing the user that neither passwords are correct.
This is my code:
Rich (BB code):
On Error GoTo Err1
Workbooks.Open Filename:="C:\MyFile\MyFile.xlsx", ReadOnly:=True, PASSWORD:="FirstPW"
' Do something
SomeLine:
On Error GoTo 0
' do something else
Exitpoint:
On Error GoTo 0
Exit Sub
Err1:
Resume NextLine ' Looks weird to resume to the line immediately following this line but I think it is necessary.
NextLine:
On Error GoTo 0 ' Is this necessary to reset the error handling status?
On Error GoTo Err2
Workbooks.Open Filename:="C:\MyFile\MyFile.xlsx", ReadOnly:=True, PASSWORD:="SecondPW"
GoTo SomeLine ' Putting Resume SomeLine instead does NOT work
Err2:
MsgBox Prompt:="Both passwords don't work."
Resume Exitpoint
Is the code OK or does it need tidying?
As far as I am aware, every time you handle an error, you must use the Resume word instead of GoTo. However in my example, I tried:
Rich (BB code):
Resume SomeLine
but it didn't go to the line SomeLine. Instead it went to the next line in the code.
Also does putting:
Rich (BB code):
On Error GoTo 0
reset the error handling status?
Thanks
Last edited: