下面的代码将根据提供给过程的参数,更改已关闭文件的ReadOnly属性,并将其ReadOnly属性设置为True或False。
Private Sub SetReadOnlyProperty(Fn As String, _
ByVal ReadOnly As Boolean)
' 21 Nov 2017
Dim Fso As Object
Dim Doc As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Doc = Fso.GetFile(Fn)
If (Doc.Attributes And vbReadOnly) <> Abs(Int(ReadOnly)) Then
Doc.Attributes = Doc.Attributes Xor vbReadOnly
End If
End Sub
此过程需要访问MS脚本运行时DLL。通过从VBE窗口中的Tools> References选中Miscrosoft Scripting Runtime
框来启用此访问。以下是如何调用该函数的示例。请注意,如果提供的文件不存在,则会导致错误。
Private Sub TestReadOnly()
SetReadOnlyProperty "H:\Test Folder\Test File.docx", False
End Sub
您需要打开文件“只读”和“另存为”一个可编辑的文档。使用'Kill'命令删除原始文件。 – Variatus
@Variatus,请您详细说明我需要执行另存为可编辑文档的命令吗?这里是我曾尝试过的,没有工作(新文档也以只读方式打开):
'Set oWd = Documents.Open(origFile)' 'oWd.SaveAs2 FileName:=newFile, ReadOnlyRecommended:=False' 'oWd.Close' 'Set oWd = Documents.Open(newFile)'
– user3617389另外,对不起,我无法弄清楚我的评论linebreaks :(。 – user3617389