title: ThisWorkbook.Protect and Unprotect in Close and Open events
body: <p>I want to unprotect a workbook immediately upon opening it, if it is protected the last time it was closed. I place the following code at the very beginning of the Open event:</p>
  
  <pre><code>If ThisWorkbook.ProtectStructure = True Then
      Call ThisWorkbook.Unprotect("openpassword")
  End If
  </code></pre>
  
  <p>Similarly, I want to protect the workbook right before it is closed. So I place the following codes at the very end of the beforeClose event:</p>
  
  <pre><code>If ThisWorkbook.ProtectStructure = False Then
      Call ThisWorkbook.Protect("openpassword",True, False)
  End If
  
  ThisWorkbook.Save
  </code></pre>
  
  <p>Neither of those two works! Meaning, if a workbook is already protected upon opening, none of the code in Open event is executed. Not even the unprotect call!</p>
  
  <p>Similarly, in the beforeClose event, all codes <strong>before</strong> the protection part are executed, except the protection part and any codes following it. I have confirmed that by placing it in different places in the beforeClose event.</p>
  
  <p>Anyone could help me with this?</p>
  
  <p>Additional explanation why I want to do this: the workbook is being shared among users with different level of authorized access. Most users face a protected workbook when allowed to work on only certain sheets. Hence, depending on who uses it last, the workbook could be closed in a protected state or unprotected state. If it is unprotected during the usage, I need to protect it immediately upon closing, so that the next unauthorized users will open a protected workbook.</p>