Pages

Friday, May 04, 2007

Changing File Attributes

SetAttr Function - Modifying File Atrributes

Sub Changing_File_Attributes()

' Using the SetAttr Function, We can set the file as read-only or hidden etc

' Make the file Read-Only
SetAttr "c:\temp\Sample.txt", vbReadOnly

' Make the file Hidden
SetAttr "c:\temp\Sample.txt", vbHidden

' Please note that if you change one attribute, the existing attribute is overwritten. For making a file as both readonly and hidden use both attributes in the function
SetAttr "c:\temp\Sample.txt", vbHidden + vbReadOnly

' Remove all atributes - convert a read-only file to read-write file, unhide the file etc
SetAttr "c:\temp\Sample.txt", vbNormal

End Sub

5 comments:

  1. Anonymous5:28 PM

    set to read only, on-off (re: attribute set)

    SetAttr "c:\temp\Sample.txt", vbReadOnly + vbArchive
    SetAttr "c:\temp\Sample.txt", vbArchive

    ReplyDelete
  2. Anonymous4:02 PM

    i need for it to turn readonly OFF to write to file then turn readonly ON

    ReplyDelete
  3. Perfect! ou can, in fact, define a string variable like so :
    ActiveWorkbook.SaveAs Filename:=Path & name1 & "-" & mydate & ".xlsx", FileFormat:=51
    myfilename = ActiveWorkbook.FullName
    SetAttr myfilename, vbReadOnly

    ReplyDelete
  4. Anonymous12:40 AM

    Hi, thank you for this post.
    Is there any way to get or set other attributes like Title , or Description or tags? I'm working with jpg files.

    ReplyDelete
    Replies
    1. Anonymous10:24 PM

      Extract JPEG / JPG file property using VBA - https://social.msdn.microsoft.com/Forums/en-US/471d0987-ed1e-438e-a561-9ce05c36a01f/getting-file-properties-for-a-file-in-excel-vba?forum=isvvba

      Delete