In an account-sheet i have used code found in this thread
http://www.vbaexpress.com/forum/showthread.php?t=11572
to make sure that users save account-sheets in right drive and folder and with a filename that identify the sheet correctly.
In this thread
http://www.vbaexpress.com/forum/showthread.php?t=19397
I've got some help to change the code to my purpose.
In some rare instances the given drive G: is not accessible (2 times in the last 6 month). The drive is on a fileserver, and it fails when netconnection fail.
In these cases users will give up saving the account sheet, and their work will be lost.
To resolve this i need code to check if the drive is accessible, and if it is not accessible, change the saving path to the desktop like this:
[vba]"current users desktop" & Sheets("Kasserapport").Range("D2").Text _
& Format(Sheets("Kasserapport").Range("A4"), "mm-yyyy") & _
" " & Sheets("Kasserapport").Range("D2").Text & ".xls"[/vba]
OS is Windows XP Pro DK
Can anyone help me with that?
Here is the code for saving as it is now:
[vba]Sub GemSom()
Dim ws As Worksheet
Set ws = Sheets("Kasserapport")
If Test(ws) = False Then
With Sheets("Kasserapport")
ActiveWorkbook.SaveAs CheckMakePath("G:\" & _
Sheets("Kasserapport").Range("H4").Text & "-huset" & "\" & "Beboere" & "\" & _
Sheets("Kasserapport").Range("D2").Text & "\" & "Regnskab" & "\" & _
Format(Sheets("Kasserapport").Range("A4"), "yyyy")) & _
"Regnskab " & Format(Sheets("Kasserapport").Range("A4"), "mm-yyyy") & _
" " & Sheets("Kasserapport").Range("D2").Text & ".xls"
End With
Else
MsgBox ("Udfyld f?rst Beboernavn og Startdato.")
End If
End Sub
Function Test(ws As Worksheet) As Boolean
If ws.Range("A4") = "" Or ws.Range("A4") = "01.01.00" Or ws.Range("D2") = "" Or ws.Range("D2") = "V?lg Beboernavn" Then Test = True
End Function
Function CheckMakePath(ByVal vPath As String) As String
Dim PathSep As Long, oPS As Long
If Right(vPath, 1) <> "\" Then vPath = vPath & "\"
PathSep = InStr(3, vPath, "\") 'Position af drev-seperatoren i stien
If PathSep = 0 Then Exit Function 'Ugyldig sti
Do
oPS = PathSep
PathSep = InStr(oPS + 1, vPath, "\") 'Position af folder
If PathSep = 0 Then Exit Do
If Len(Dir(Left(vPath, PathSep), vbDirectory)) = 0 Then Exit Do 'check stien
Loop
Do Until PathSep = 0
MkDir Left(vPath, PathSep)
oPS = PathSep
PathSep = InStr(oPS + 1, vPath, "\")
Loop
CheckMakePath = vPath
End Function[/vba]
Underlined code is highlighted when trying to save with the drive not accessible!