My codes to open password protected workbook doesn't work

ashley12

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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try using
VBA Code:
Workbooks.Open Filename:= "C:\................", Password:="password"
Make sure the Case of "password" is correct as well !!
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,960
Latest member
AKSMITH

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