Consulting

Results 1 to 13 of 13

Thread: Test if Current Dir is Read Only

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Test if Current Dir is Read Only

    I looked up in the help for this and I can't figure it out. I want to check if the current directory is read only:

    this is what I tried:

    [VBA]If CurDir.GetAttr.vbReadOnly = True Then
    MsgBox "Test"
    End If[/VBA]

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi again,

    [vba] If (GetAttr(CurDir) And vbReadOnly) <> 0 Then
    MsgBox CurDir & " is read only."
    Else
    MsgBox CurDir & " is not read only."
    End If[/vba]
    Matt

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    If GetAttr(CurDir) And vbReadOnly Then
    MsgBox "Test"
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    This is the code that I am using to save:

    [VBA]Sub SaveCode(Optional ByVal stgDirectory As String = "H:")

    errUpdateLogFile "Sub-" & "SaveCode", stgDirectory
    Dim wbFileName As String

    On Error GoTo SaveNoDirectory
    ChDrive "H:"
    ChDir stgDirectory 'Change Directory to the users default directory
    SaveNoDirectory:

    'If the user hasn't set a Default Directory
    wbFileName = Application.GetSaveAsFilename

    On Error GoTo SaveBook
    If wbFileName = "False" Then
    MsgBox "The file was not saved."
    errUpdateLogFile "SaveCode-NoSave"
    Exit Sub
    End If

    SaveBook:

    'Check if Directory the user is trying to save in is Read-Only
    If GetAttr(CurDir) And vbReadOnly Then
    MsgBox "Test"
    End If

    'If the user didn't type the file extension, this will add it
    If Right(wbFileName, 4) = ".xls" Then
    ActiveWorkbook.SaveAs wbFileName, FileFormat:=xlNormal
    Else
    ActiveWorkbook.SaveAs wbFileName & "xls", FileFormat:=xlNormal
    End If

    errUpdateLogFile "SaveCode-SaveDefault"
    Exit Sub
    End Sub[/VBA]

    It tests if the user has set a default save location and then it saves it. However, I am trying to include a check for the user in case they try to save in a read-Only location, so the progrmam won't crash. The code you showed me I thick is checking the actual default location, not the location the user just chose. Sorry I should have given more information when I asked.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    So chnage CurDir in the test to your directory.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I know that is what I have to do but how do I figure out what directory that the user has chosen from the

    [VBA]Application.GetSaveAsFilename [/VBA]

    I know how to get the file name but not the directory

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's in wbFilename

    [vba]

    MsgBox Left(wbFilename, InStrRev(wbFilename, "\") - 1)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    This is what I have now:

    [VBA]'If the user hasn't set a Default Directory
    wbFileName = Application.GetSaveAsFilename

    On Error GoTo SaveBook
    If wbFileName = "False" Then
    MsgBox "The file was not saved."
    errUpdateLogFile "SaveCode-NoSave"
    Exit Sub
    End If

    SaveBook:

    'Get the User specified directory
    stgSelectedDirectory = Left(wbFileName, InStrRev(wbFileName, "\") - 1)

    'Check if Directory the user is trying to save in is Read-Only
    If GetAttr(stgSelectedDirectory) And vbReadOnly Then
    MsgBox "Test"
    End If[/VBA]

    Unfortunately, it isn't working.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do you want to give us a clue as to what 'not working' means?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    even if the folder is read only doesn't show the msgbox. It skips over the if statement as if the folder isn't read only.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Did you check that stgSelectedDirectory is giving the value you expected?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Yeah I would msgbox the directory variable anyways to make sure it is populating correctly.

    To verify the GetAttr function is working right, run this, you should only see "true1"[vba]Sub ReadOnlyTest()
    MkDir "C:\rndm1"
    MkDir "C:\rndm2"
    SetAttr "C:\rndm1", vbReadOnly
    If GetAttr("C:\rndm1") And vbReadOnly Then MsgBox "true1"
    If GetAttr("C:\rndm2") And vbReadOnly Then MsgBox "true2"
    SetAttr "C:\rndm1", vbNormal
    RmDir "C:\rndm1"
    RmDir "C:\rndm2"
    End Sub[/vba]
    Matt

  13. #13
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Matt,

    All I saw was "true1".

Posting Permissions

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