Using VBA to open Password Protected Files

bajwali

New Member
Joined
May 12, 2020
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi very new to VBA and coding so all help would be very appreciated.

So i have about 37 files which i need to open with VBA and then take some data and aggregate it into a summary however some of the files are password protected thankfully with the same password.

I am using the Application.Get Open file name to get the location of all the files and then usings a loop from 1 to n to open the FileNames(i) what can i do so that as the workbook is opened and it asks for the password it types in the password "password1".

Sub Aggregation_Data()
Dim FileNames As Variant, i As Integer, j As Integer
Dim TWB As Workbook, aWB As Workbook



Set TWB = ThisWorkbook

MsgBox ("Enter Files that you wish to import data from")

FileNames = Application.GetOpenFilename(FileFilter:="Excel Filter (*.xlsx), *xlsx", Title:="Open File(s)", MultiSelect:=True)

For i = 1 To UBound(FileNames)

Workbooks.Open FileNames(i)

Next i

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Maybe this
VBA Code:
Sub Aggregation_Data()
Dim FileNames As Variant, i As Integer, j As Integer
Dim TWB As Workbook, aWB As Workbook
Set TWB = ThisWorkbook
MsgBox ("Enter Files that you wish to import data from")
FileNames = Application.GetOpenFilename(FileFilter:="Excel Filter (*.xlsx), *xlsx", Title:="Open File(s)", MultiSelect:=True)
For i = 1 To UBound(FileNames)
Workbooks.Open FileNames(i)
ActiveWorkbook.Unprotect Password:="myPassword"
'do your code here
ActiveWorkbook.Protect Password:="myPassword"
Next i
End Sub
 
Upvote 0
Hi this did not work as the password box opens up and the code is not able to put the password into the box

Pic.PNG
 
Upvote 0
This way then
VBA Code:
Sub Aggregation_Data()
Dim FileNames As Variant, i As Integer, j As Integer
Dim TWB As Workbook, aWB As Workbook
Set TWB = ThisWorkbook
MsgBox ("Enter Files that you wish to import data from")
FileNames = Application.GetOpenFilename(FileFilter:="Excel Filter (*.xlsm), *xlsm", Title:="Open File(s)", MultiSelect:=True)
For i = 1 To UBound(FileNames)
Workbooks(i).Unprotect Password:="password"
'do your code here
Workbooks(i).Protect Password:="password"
Next i
End Sub
 
Upvote 0
Hi Tried that does not work as well.


Had to change to xlsx in the filenames variable Application.GetOpenfilename as otherwise i could not allow me to choose the files.

However after that it just runs the code without unprotecting the file it almost skips over the file.

So with this it does not open the file and hence nothing happens
VBA Code:
Workbooks(i).Unprotect Password:="password"


2nd try.PNG
 
Upvote 0
I can also work with something which allows me to select the files using the Application.GetOpenfilename and then unprotect the files and save them in another location and then run my code as a second step to get the data.
 
Upvote 0
I have come across this property which might be the key to solve this can anyone more experience of these things than me please guide me on if this can be used to solve my issue and if so how?
Application.Dialogs (xlDialogWorkbookProtect)
 
Upvote 0
Hi

So i kept playing with it and it seems i have at least figured out the problem i am facing. If i give the full file path and then the password the excel file is opened fine no issues.

However as i am letting the user choose the files and then using FileNames(i) that is what is causing it to fail. So when i give VBA the code
VBA Code:
"C:\Users\ali.bajwa\OneDrive \XXXX" , Password:="password"
it works however not with
VBA Code:
Workbooks.Open FileNames(i) Password:="password"

All help would be very useful as this is really bugging me



VBA Code:
Sub Aggregation_Data()
Dim FileNames As Variant, i As Integer, j As Integer
Dim TWB As Workbook, aWB As Workbook
Set TWB = ThisWorkbook
MsgBox ("Enter Files that you wish to import data from")
FileNames = Application.GetOpenFilename(FileFilter:="Excel Filter (*.xlsx), *xlsx", Title:="Open File(s)", MultiSelect:=True)
For i = 1 To UBound(FileNames)
Workbooks.Open FileNames(i)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top