PDA

View Full Version : Test if Current Dir is Read Only



Djblois
09-10-2007, 12:06 PM
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:

If CurDir.GetAttr.vbReadOnly = True Then
MsgBox "Test"
End If

mvidas
09-10-2007, 12:27 PM
Hi again,

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

Bob Phillips
09-10-2007, 12:29 PM
If GetAttr(CurDir) And vbReadOnly Then
MsgBox "Test"
End If

Djblois
09-10-2007, 12:57 PM
This is the code that I am using to save:

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

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.

Bob Phillips
09-10-2007, 02:08 PM
So chnage CurDir in the test to your directory.

Djblois
09-11-2007, 06:25 AM
I know that is what I have to do but how do I figure out what directory that the user has chosen from the

Application.GetSaveAsFilename

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

Bob Phillips
09-11-2007, 06:27 AM
It's in wbFilename



MsgBox Left(wbFilename, InStrRev(wbFilename, "\") - 1)

Djblois
09-11-2007, 08:58 AM
This is what I have now:

'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

Unfortunately, it isn't working.

Bob Phillips
09-11-2007, 12:19 PM
Do you want to give us a clue as to what 'not working' means?

Djblois
09-11-2007, 12:22 PM
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.

Bob Phillips
09-11-2007, 12:24 PM
Did you check that stgSelectedDirectory is giving the value you expected?

mvidas
09-12-2007, 09:14 AM
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"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

Djblois
10-12-2007, 12:16 PM
Matt,

All I saw was "true1".