title: Excel VBA Workbook.ChangeFileAccess
body: <p>I'm having trouble with changing the file access mode on Excel workbooks using VBA. I am using Office 2010.  </p>
  
  <p>I want to be able to alternate between read only and read write mode as appropriate. However it seems changing a workbook from read/write to read only and then back again as in the code below causes an automation error when trying to access any member of the workbook object.</p>
  
  <pre><code>Public Sub example()
  
  Dim w As Workbook
  
    'open workbook with write access
    Set w = Workbooks.Open("example.xlsx", ReadOnly:=False)
    Debug.Print w.Name 'sucessfully accesses members of w
  
    'change file access to read only
    w.ChangeFileAccess XlFileAccess.xlReadOnly
    Debug.Print w.Name 'successfully accesses members of w
  
    'change file access back to read/write
    w.ChangeFileAccess XlFileAccess.xlReadWrite
    Debug.Print w.Name 'fails to access members of w with automation error
  
  End Sub
  </code></pre>
  
  <p>I don't understand why this is the case. It is obviously perfectly possible to open a workbook as read only, change it to read write and then continue to use the object. Why is this situation different? I can find no mention of this behaviour on MSDN.<br>
  <a href="http://msdn.microsoft.com/en-us/library/ff193344.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ff193344.aspx</a>  </p>
  
  <p>Is it possible to change between file access modes as I am trying to do?</p>