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

3 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

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group