vba to open enripted password excel file

Please see the attached excel. I went to options > encrypt with password. Then in VBA I try to open the file using this code:
Set wkb = objXL.Workbooks.Open(mypathFile, , , , password:="password", WriteResPassword:="password")
But it says incorrect password, error 1004. Can you please help gurus

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
There several, free, password retriever's for Excel on the web.
RayneAuthor Commented:
U don't to retrieve any password ...since I know the password ......my question is how in vba code I can open that file ....
RayneAuthor Commented:
The password is password
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Martin LissOlder than dirtCommented:
I think you had the password parameter in the wrong place. This works. Don't forget to change C:\temp to match your path.

Set wkb = Workbooks.Open("C:\temp\withEncriptedPassword.xlsx", , , , "password", "password")
RayneAuthor Commented:
RayneAuthor Commented:
i am using access vba to open this excel file just FYI
RayneAuthor Commented:
so your code is not working i got the issue
Martin LissOlder than dirtCommented:
What method did you use to password protect the workbook? I created a workbook and saved it with a password by way of the File->Save As->Tools->General Options and when I did that I was able to open it with this code in Access.

I attached my workbook.

Dim objXL As New Excel.Application
Dim wkb As Excel.Workbook
objXL.Visible = True
Set wkb = objXL.Workbooks.Open("C:\temp\MyWorkbook.xlsx", Password:="password")

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RayneAuthor Commented:
Martin is my HERO :)
RayneAuthor Commented:
thank you
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

For my own interest can you tell me what the problem was?

Marty - MVP 2009 to 2015
RayneAuthor Commented:
could be two things - using one comma instead of four like this:
withEncriptedPassword.xlsx", , , , "password",

another - i was storing the password in a variable and using the variable in the last code you gave
Martin LissOlder than dirtCommented:
Are you asking for more help? If so then please explain what you need.
RayneAuthor Commented:
Martin  i am not asking any help. I tried to answer your last question as to what resolved the issue :)
Martin LissOlder than dirtCommented:
Sorry, I forgot I asked you that:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.