one line macro to enter password for workbook

  • Thread starter Thread starter N Lenox
  • Start date Start date
N

N Lenox

Hi All --

What is the one line of code that will enter a password
upon opening a workbook (assuming macros are enabled)?

Thanks.
 
Hello Experts,
I have the same question...Opening a workbook that's needs a password
first in order to open it. But this code still doesn't work. Any
ideas?

Workbooks.Open Filename:="C:\My Documents\File1.xls"
ActiveWorkbook.Unprotect "test"

I've tried no quotations, brackets, no brackets, around the password but
of no avail. The popup for the password still surfaces. And, I do have
the correct password.

I've even tried:
Dim Password as variant
Password = "test"
Workbooks.Open Filename:="C:\My Documents\File1.xls"
ActiveWorkbook.Unprotect Password

It still prompts me to enter the password manually to open the file.
Any ideas?

Thanks in advance,
Ricky
 
Ricky,

Dim wkbk as string, pass as string
wkbk = "?????" ' include path
pass = "mypassword"
Workbooks.Open FileName:=wkbk, password:=pass
 
Hi Steve,
Thanks! That's the ticket. I used this one-liner:

Workbooks.Open Filename:="C:\My Documents\File1.xls", Password:="test"

Just wondering, what's the difference between this one-liner and
Dimensioning other than setting the filename path and password up front
so that it can be recalled at a later point in the code, if needed?

Dim wkbk as string, pass as string
wkbk = "?????" ' include path
pass = "mypassword"

Thanks again,
Ricky
 
Ricky,

You got it. Also in my application I am opening multiple workbooks and
need to set the password for each. So that pass = a reference to a list and
changes in a loop.

Glad it worked for you...
 
Back
Top