我正在为word和excel编写一个宏。输出是应该保存的生成的文件。因为这些文件已经存在并且是写保护的,所以我必须以某种方式移除保护。我在excel中发现了一个取消保护的功能,但它并不真正起作用。我也没有找到任何通过命令o0取消保护的命令
在保存新文件之前,我考虑过删除这些文件。但我想找到一个解除保护的解决方案。
发布于 2012-08-28 14:45:05
尝试使用cmd中的attrib命令更改文件属性,attrib *.xls -r
将帮助您删除只读属性。
发布于 2012-08-28 14:56:20
下面是使用FileSystemObject从VBA中删除只读属性的方法:
Sub RemoveReadOnly(filePath As String)
Dim FSO As FileSystemObject
Dim f As Scripting.File
Set FSO = New FileSystemObject
Set f = FSO.GetFile(filePath)
If fil.Attributes And ReadOnly Then 'It's read-only. Remove that attribute.
fil.Attributes = fil.Attributes - ReadOnly
End If
End Sub
用法:
RemoveReadOnly "C:\mydir\myReadOnlyFile.dat"
更多信息:http://msdn.microsoft.com/en-us/library/5tx15443%28v=vs.85%29.aspx
注意:以上操作需要如下设置引用:工具>引用...>选中Microsoft Scripting Runtime旁边的复选标记。
如果你不想设置一个引用,那就改用后期绑定:
Sub RemoveReadOnly(filePath As String)
Dim FSO As Object
Dim fil As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fil = FSO.GetFile(filePath)
If fil.Attributes And 1 Then '1 = ReadOnly
fil.Attributes = fil.Attributes - 1
End If
End Sub
https://stackoverflow.com/questions/12153976
复制相似问题