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]
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]
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
[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
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.
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
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
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
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.
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
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.
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
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
Matt,
All I saw was "true1".