Public Function ChangeFileAttributes(ByVal pstrFile As String, _ ByVal pstrAttributes As String, _ ByVal pblnOn As Boolean _ ) As Boolean Dim fso As FileSystemObject Dim intAttributes As Integer Dim intI As Integer Dim objFile As File For intI = 1 To Len(pstrAttributes) Select Case Mid$(pstrAttributes, intI, 1) Case "N" 'Normal file. No attributes are set. intAttributes = intAttributes + 0 Case "R" 'Read-only file. Attribute is read/write. intAttributes = intAttributes + 1 Case "H" 'Hidden file. Attribute is read/write. intAttributes = intAttributes + 2 Case "S" 'System file. Attribute is read/write. intAttributes = intAttributes + 4 Case "A" 'Archive. File has changed since last backup. Attribute is read/write. intAttributes = intAttributes + 32 End Select 'Other attributes (Read-only) 'Volume 8 Disk drive volume label. Attribute is read-only. 'Directory 16 Folder or directory. Attribute is read-only. 'Alias 64 Link or shortcut. Attribute is read-only. 'Compressed 128 Compressed file. Attribute is read-only. Next intI 'Renverse les attributs s'il faut les mettre à OFF If Not pblnOn Then intAttributes = intAttributes * -1 Set fso = New FileSystemObject If fso.FileExists(pstrFile) Then Set objFile = fso.GetFile(pstrFile) objFile.Attributes = objFile.Attributes + intAttributes End If End FunctionThis is how to use it:
'Set attributes to Read-Only Call ChangeFileAttributes(pstrFileName, "R", True)