title: Check whether Excel file is Password protected
body: <p>I am trying to open an Excel (xlsm) file via VBA. It may or may not be protected with a (known) password. I am using this code:</p>
  
  <pre><code>On Error Resume Next
  Workbooks.Open filename, Password:=user_entered_pw
  opened = (Err.Number=0)
  On Error Goto 0
  </code></pre>
  
  <p>Now, this works fine if the workbook has a password. But if it is unprotected, it can NOT be opened. Apparently this is a bug in XL2007 if there is also workbook structure protection active. (<a href="http://vbaadventures.blogspot.com/2009/01/possible-error-in-excel-2007.html" rel="nofollow noreferrer">http://vbaadventures.blogspot.com/2009/01/possible-error-in-excel-2007.html</a>). On old XL2003, supplying a password would open both unprotected and password protected file.</p>
  
  <p>I tried:</p>
  
  <pre><code>Workbooks.Open filename, Password:=user_entered_pw
  If (Err.Number &lt;&gt; 0) Then workbooks.open filename
  </code></pre>
  
  <p>This works for unprotected and protected file. However if the user enters a wrong password it runs into the second line and pops up the "enter password" prompt, which I do not want.</p>
  
  <p>How to get around this?</p>