PDA

View Full Version : Change attributes of a folders contents



Tezzies
11-27-2007, 07:44 AM
Hello all

I have a folder with a selection of word and excel files in there.

I need some code which will check within the folder to see if a file exists and if so changes the attribute to read only.

Many thanks in advance

lucas
11-27-2007, 09:43 PM
Hi Tezzies,
Only have a minute but will pass you this bit of code to check if a workbook or directory exist. Change the file attribute to read only is fairly easy once you find the file.

Option Explicit
Function wbOrDirExists(PathName As String) As Boolean
'Macro Purpose: Function returns TRUE if the specified file
' or directory exists, false if not.
'File usage : Provide full file path and extension
'Path usage : Provide full file path only (accepts with/without trailing "\")
Dim sTemp As String
Application.Volatile
'Ignore errors to allow for error evaluation
On Error Resume Next
sTemp = GetAttr(PathName)

'Check if error exists and set response appropriately
Select Case Err.Number
Case Is = 0
wbOrDirExists = True
Case Else
wbOrDirExists = False
End Select

'Resume error checking
On Error GoTo 0
End Function
Sub TestIt()
'Macro Purpose: To test the wbOrDirExists function
Dim sPath As String
'Change your directory here
sPath = "C:\Test.xls"
'Test if directory or file exists
If wbOrDirExists(sPath) Then
'your code here instead of the messagebox
MsgBox sPath & " exists!"
Else
MsgBox sPath & " does not exist."
End If
End Sub
just add your code to change the attributes after the if then statement. You can search the forum for the code to change the attributes

Tezzies
11-28-2007, 01:28 AM
works a treat, many thanks for quick helpful response.:beerchug: