My codes to open password protected workbook doesn't work

ashley12

New Member
Joined
Dec 6, 2020
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

Below is my codes. It does try to open the file, but it keeps asking me for password although I already include the password in the codes. What have I got wrong?

VBA Code:
Sub openfile()
  Dim wb As Workbook
    
  Set wb = Workbooks.Open(FileName:="C:\...", Password:="password")

End Sub

Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,118
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Try using
VBA Code:
Workbooks.Open Filename:= "C:\................", Password:="password"
Make sure the Case of "password" is correct as well !!
 

ashley12

New Member
Joined
Dec 6, 2020
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Thanks Michael, I know what the problem is now. If the workbook is closed, the codes work fine and open the file. But if it's opened by another user then it ask for my password. However, I want it to send a message box say "File is opened by another user, please try again later". Do you know how to do that?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,118
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Maybe this way
You can use this function

VBA Code:
Function BookOpen(wbName As String) As Boolean
On Error Resume Next
BookOpen = Len(Workbooks(wbName).Name)
End Function

and call it like this

VBA Code:
If BookOpen("test.xls") Then
MsgBox "File is open"
Else
'code to open file
End If
 

Watch MrExcel Video

Forum statistics

Threads
1,129,387
Messages
5,636,020
Members
416,892
Latest member
Bensch

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