PoeticRobot
New Member
- Joined
- Nov 19, 2019
- Messages
- 10
- Office Version
- 365
- Platform
- Windows
I have the below code, which I will use to open a file. I will then gather data about the file and report back in a master workbook.
My issue comes when I am trying to open a file which has a password:
For clarity: intPassword is going to be used to define whether the workbook I am opening has a password, and if so, if the password saved as strPass is correct.
I am using Office 365 ProPlus on Windows 10.
My issue comes when I am trying to open a file which has a password:
- The first Workbooks.Open method presents me with a pop-up window, which I expect, although I will need to suppress this pop-up window if the (known incorrect) password does not open the workbook.
- The second Workbooks.Open uses the password parameter, and I know the password used is correct, but I am still being asked to enter the password, rather than VBA opening the workbook.
For clarity: intPassword is going to be used to define whether the workbook I am opening has a password, and if so, if the password saved as strPass is correct.
0 = No Password, 1 = Password (strPass), 2 = Password (Unknown)
VBA Code:
Public Sub Main()
Dim strFilePath As String
Dim strFileName As String
Dim wbkTarget As Workbook
Dim intPassword As Integer 'Code to show if file is password protected, and do I know the password.
Dim strPass As String
strFilePath = "MyFilePath"
strFileName = "MyFileName"
strPass = "Password"
On Error GoTo Proc_Err
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wbkTarget = Nothing
On Error Resume Next
Application.EnableEvents = False 'Prevent OnOpen Events in wbkTarget
Set wbkTarget = Workbooks.Open(Filename:=strFilePath & strFileName)
If Not wbkTarget Is Nothing Then
intPassword = 0
GoTo FileOpen
End If
Set wbkTarget = Workbooks.Open(Filename:=strFilePath & strFileName, UpdateLinks:=False, Password:="strPass", IgnoreReadOnlyRecommended:=True)
If Not wbkTarget Is Nothing Then
intPassword = 1
FileOpen:
'Code goes here to get details of open file
wbkTarget.Close
Else
intPassword = 2
With ThisWorkbook.Sheets("Sheet1")
'Process Details
End With
End If
Application.EnableEvents = True
Set wbkTarget = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Proc_Exit:
Exit Sub
Proc_Err:
Debug.Print Err.Description
Resume Proc_Exit
End Sub
I am using Office 365 ProPlus on Windows 10.