Make File Writable macro

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

We use MKS to archive our code and always set the local copy to read-only
when it is locked. Sometimes either another person has a file locked that I
need to modify or I want to simply try out a change without modifying the
project. For that I need to simply remove the read-only attribute from my
file. Using a DOS box or Explorer is time consuming at best. Since I had
used a macro in Borland for this purpose, I was spoiled and needed one in
Visual Studio. Here is a simple one that does the trick.

Sub MakeFileWriteable()
‘DESCRIPTION: Remove Read-Only File attribute from active document

‘ Author: V. Sauder
‘ Copyright: Hekimian Labs, Inc. 1999

Dim doc
Set doc = ActiveDocument
Dim fname
fname = doc.FullName
if (doc.ReadOnly) then
doc.ReadOnly = False
msg = ToggleReadOnlyBit(fname)
‘MsgBox msg
end if
Set doc = nothing

End Sub

const ReadOnly = 1
Function ToggleReadOnlyBit(filespec)

‘ Copyright Microsoft. VBscript example code slighty modified.

Dim oFS, oFile
Set oFS = CreateObject(“Scripting.FileSystemObject”)
Set oFile = oFS.GetFile(filespec)
oFile.attributes = oFile.attributes xor ReadOnly
If oFile.attributes and ReadOnly Then
ToggleReadOnlyBit = “ReadOnly bit is now set.”
Else
ToggleReadOnlyBit = “ReadOnly bit is now cleared.”
End If

End Function

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read