My codes to open password protected workbook doesn't work

ashley12

New Member
Joined
Dec 6, 2020
Messages
35
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
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,028
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
35
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,028
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,127,145
Messages
5,623,003
Members
415,945
Latest member
Koboca

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