admin管理员组文章数量:1315375
I have to edit an excel file with vba code for an old Excel 2003 pack in an old machine. I have the necessity to check, while opening the file, if it has "read-only" attribute. If yes I have to remove this attribute and save the file itself in the same location,like overwriting it, by removing the read-only version to an editable one.
I wrote this function to remove the READ-ONLY:
Sub RemoveReadOnly(filePath As String)
Dim FSO As Object
Dim file As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set file = FSO.GetFile(filePath)
If file.Attributes And 1 Then '1 = ReadOnly
file.Attributes = file.Attributes - 1
End If
End Sub
Then in the Workbook_Open function i added this code:
If ThisWorkbook.ReadOnly Then
Application.DisplayAlerts = False
RemoveReadOnly wb.FullName 'wb = ThisWorkbook
ThisWorkbook.SaveAs ThisWorkbook.FullName, ReadOnlyRecommended:=False
Application.DisplayAlerts = True
End If
I'm sure i'm missing something or maybe I have to try a different way out.
Can someone help me overwriting the file itself or give me another way rather the one I'm trying to develop?
Thank you guys.
I have to edit an excel file with vba code for an old Excel 2003 pack in an old machine. I have the necessity to check, while opening the file, if it has "read-only" attribute. If yes I have to remove this attribute and save the file itself in the same location,like overwriting it, by removing the read-only version to an editable one.
I wrote this function to remove the READ-ONLY:
Sub RemoveReadOnly(filePath As String)
Dim FSO As Object
Dim file As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set file = FSO.GetFile(filePath)
If file.Attributes And 1 Then '1 = ReadOnly
file.Attributes = file.Attributes - 1
End If
End Sub
Then in the Workbook_Open function i added this code:
If ThisWorkbook.ReadOnly Then
Application.DisplayAlerts = False
RemoveReadOnly wb.FullName 'wb = ThisWorkbook
ThisWorkbook.SaveAs ThisWorkbook.FullName, ReadOnlyRecommended:=False
Application.DisplayAlerts = True
End If
I'm sure i'm missing something or maybe I have to try a different way out.
Can someone help me overwriting the file itself or give me another way rather the one I'm trying to develop?
Thank you guys.
Share Improve this question asked Jan 30 at 10:53 bsiilviabsiilvia 971 silver badge10 bronze badges 3 |1 Answer
Reset to default 0I found a solution that also manage if the READ-ONLY attribute is set on the file parent folder.
To check if the function RemoveReadOnly
worked without errors I set up a variable Dim checkDirAttr As Boolean
and manages as you can see in the code here:
Sub RemoveReadOnly(filePath As String)
Dim FSO As Object
Dim file As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set file = FSO.GetFile(filePath)
On Error GoTo Errore
If file.Attributes And 1 Then '1 = ReadOnly
file.Attributes = file.Attributes - 1
End If
Errore:
checkDirAttr = True
MsgBox "There's a problem!"
Exit Sub
End Sub
As you can see I set the checkDirAttr
to true if I got an error like when the file folder is inaccessible.
In my Workbook_Open sub I used this code:
If ThisWorkbook.ReadOnly Then
Application.DisplayAlerts = False
'get the current file path
oldFile = ThisWorkbook.FullName
'get and save the path for my temp file
sDefaultFilePath = Application.DefaultFilePath
sFilePath = sDefaultFilePath & "\TEMP-FILE.xls"
'call my function to remove the READ-ONLY attribute
RemoveReadOnly wb.FullName
'check if I got some errors, if not...
If checkDirAttr = False Then
'save a copy of my current file to the Excel default folder
wb.SaveAs sFilePath
'Set a temp workbook with the current one so I can take the code open and running
Set wbAppo = ActiveWorkbook
'open the original file again but without the READ-ONLY attribute
Application.OnTime Now + TimeValue("00:00:05"), Application.Workbooks.Open(oldFile)
'close the temp workbook
If Application.Workbooks.Count = 1 Then
Application.Quit
Else
wbAppo.Close SaveChanges:=False
End If
Application.DisplayAlerts = True
End If
End If
Hope this will help someone else that could need to solve the same kind of issue.
Thank you again for the support!
本文标签: VBA Excel How to remove READONLY attribute from the actual fileStack Overflow
版权声明:本文标题:VBA Excel: How to remove READ-ONLY attribute from the actual file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971352a2407857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Workbooks.Add
(instead of open) and then with the saved filename, delete the original file and then just save it like normal using that saved filename? – braX Commented Jan 30 at 11:07RemoveReadOnly
removes the read only flag (set on Operating System level). No need to save the file again. If you plan to modify the file, you will need to remove the flag before you open the file. – FunThomas Commented Jan 30 at 13:16