VB and VBA Users Source Code: Change ReadOnly property of an open Excel workbook
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Change ReadOnly property of an open Excel workbook
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, June 28, 2000
Hits:
717
Category:
Office
Article:
The ReadOnly property of an open workbook is ReadOnly. If you use a program such as SourceSafe, you may often want to change a workbook to readwrite after it has been opened (as readonly). The functions listed below change the readonly properties of an open workbook. Function MakeReadWrite(sWorkbook As String, Optional bChangeReadOnly As Boolean = True) As Boolean Dim sFileFullName As String If WorkbookOpen(sWorkbook) Then On Error GoTo ErrFailed sFileFullName = Workbooks(sWorkbook).FullName If GetAttr(sFileFullName) And vbReadOnly Then If bChangeReadOnly Then 'Change to Read-Write SetAttr sFileFullName, vbNormal Else Exit Function End If End If If Workbooks(sWorkbook).ReadOnly Then Workbooks(sWorkbook).ChangeFileAccess xlReadWrite End If End If Exit Function ErrFailed: 'Failed MakeReadWrite = True End Function Function WorkbookOpen(WorkbookName As String) As Boolean On Error Resume Next WorkbookOpen = CBool(Len((Workbooks(WorkbookName).Name))) On Error GoTo 0 End Function An example of how to call the function is: If MakeReadWrite(ThisWorkbook.Name, True) Then MsgBox "Failed to Change Workbook ReadOnly Property!", vbExclamation Else ThisWorkbook.Save MsgBox "Workbook Saved!", vbExclamation End If
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder