Opening a file with vba password problem

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have written the following code to open a password protected file. But it gives a 1004 error. I can’t see what I have done wrong. Any ideas would be much appreciated.

Sub Cts()

Dim wb As Workbook
Dim receiver As String

receiver = "C:\Documents and Settings\Desktop\Receiver.xls"

Set wb = Application.Workbooks.Open("receiver", Password:="password", WriteResPassword:="password")

With wb.Sheets("ST Comment Results")


End With
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What are you doing with the with statement, is it necessary if you are only selecting the sheet.

Code:
With wb.Sheets("ST Comment Results")
 
 
End With

could be

Code:
wb.Sheets("ST Comment Results").select
 
Upvote 0
You need to pass the receiver as a variable, not as the string "receiver":

Set wb = Application.Workbooks.Open(receiver, Password:="password", WriteResPassword:="password")
 
Upvote 0
Hi, Thanks for the reply.

The code is part of a bigger piece of code. The error comes when trying to open another workbook that has a password on the ReadWrite part when the file is opened. I thought that Password:="password" was all that was needed. But I'm obviously doing something wrong.
 
Upvote 0
I'm sorry but I don't follow. I still can't get it to work. Any pointers?
 
Upvote 0
Did you try Richard's code? If so, what happened?
 
Upvote 0
HI,

It debugs on the 'Set wb' line. If I use 'Workbooks.Open Filename:="C:\Documents and Settings\Desktop\Receiver.xls"UpdateLinks:=True, writerespassword:="password" by itself file opends no problem. Strange.
 
Upvote 0
I've done it. I had enclosed receiver in inverted comma's. Doh. I'm busy jamming my hand in the draw as I type. It's teh thought of 4 days off that's doing it. Really appreciate your time, thanks.
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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