Entering password for protected WB

Luke777

New Member
Joined
Aug 10, 2020
Messages
25
Office Version
  1. 2007
Platform
  1. Windows
Hi all!

I'm using the following code to open a workbook.

VBA Code:
Workbooks.Open (fPath & fName)

fPath & fName are, of course, specified before hand but I've omitted that for simplicity.

The workbook being opened, however, is password protected (which, when running the code, it asks for after reaching the above line). Unfortunately I'm a little stumped on how to actually enter the password using VBA (instead of entering manually). This isn't usually a problem as
VBA Code:
Workbooks("Book1").Unprotect "password"
usually works.

But in my case it doesn't seem to play nicely with the fPath & fName method of opening a file (which I must use for other reasons unfortunately). Screen updating is also off if that makes any difference.

Thanks for the help!
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,473
Office Version
  1. 2019
Platform
  1. Windows
Hi,
if you mean the workbook is password protected to open? then try

VBA Code:
Workbooks.Open fpath & fname, Password:="password"

Dave
 
Solution

Luke777

New Member
Joined
Aug 10, 2020
Messages
25
Office Version
  1. 2007
Platform
  1. Windows
Thanks you two, I'd completely forgotten this method..

VBA Code:
Workbooks.Open fpath & fname, Password:="password"

Figured it out almost as soon as I posted the question lol, sorry for taking up your time!
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,473
Office Version
  1. 2019
Platform
  1. Windows
No worries glad we could help
We appreciate the feedback

Dave
 

Watch MrExcel Video

Forum statistics

Threads
1,127,009
Messages
5,622,145
Members
415,881
Latest member
tasic89

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
Top