Consulting

Results 1 to 3 of 3

Thread: Change attributes of a folders contents

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Posts
    61
    Location

    Change attributes of a folders contents

    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

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.

    [VBA]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[/VBA]
    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
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Nov 2007
    Posts
    61
    Location
    works a treat, many thanks for quick helpful response.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •