ワードとエクセルのマクロを書いています。出力は、保存する必要がある生成ファイルです。これらのファイルは既に存在し、書き込み保護されているため、どうにかして保護を解除する必要があります。Excel で保護解除機能を見つけましたが、実際には機能しません。cmd o0 で保護を解除するコマンドも見つかりませんでした
新しいファイルを保存する前に、ファイルを削除することを考えました。しかし、保護を解除するための解決策を見つけたいと思います。
cmdのattribコマンドを試してファイル属性を変更する
attrib *.xls -r
と、読み取り専用属性を削除するのに役立ちます。
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
注:上記では、次のように参照を設定する必要があります。[ツール]>[参照...]>[MicrosoftScriptingRuntime]の横にあるチェックマークを設定します。
参照を設定したくない場合は、代わりに遅延バインディングを使用してください。
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