perhol
05-19-2008, 12:32 PM
mikerickson helped me with a similar question in this thread:
http://www.vbaexpress.com/forum/showthread.php?p=143854#post143854
But in some instances it do not function as required, and now i need to redirect both 'File -> Save', 'File -> SaveAS' and the icon 'Save' in Standard Toolbar to a sub Test_Navn if macros is enabled.
This is Workbook_BeforeSave code
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
Rem if SaveAs is in progress
Application.EnableEvents = False: Rem prevent Test_Navn from triggering cascading BeforeSave's
Call Test_Navn: Rem custom save routine
Cancel = True: Rem spike normal SaveAs
Application.EnableEvents = True
End If
End Sub
And the following 7 Sub's and Functions is all code related to save.
This is a sub and a function to test if conditions is meet in 2 cells in sheet "Kasserapport"
Sub Test_Navn()
Dim ws As Worksheet
Set ws = Sheets("Kasserapport")
If Test(ws) = False Then
Call Test_G
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
This is a sub and a function, called by the sub 'Test_Navn'. They test if drive G: is present and ready.
Sub Test_G()
If DExist("g") = 2 Then
Call G_Exist
End If
If DExist("g") = 0 Then
Call G_Do_Not_Exist
End If
End Sub
Public Function DExist(OrigFile As String)
Dim fs, d
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.driveexists(OrigFile) = True Then
Set d = fs.getdrive(OrigFile)
DExist = 1
If d.isready = True Then
DExist = 2
Exit Function
End If
Else
DExists = 0
End If
End Function
This is a sub and function called if drive G: is present and ready.
Sub G_Exist()
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
MsgBox "Regnskabet er gemt" + Chr(10) + _
"i beboerens regnskabsmappe p? G:-drevet"
End Sub
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
This is a sub called if drive G: is not present and ready.
Sub G_Do_Not_Exist()
ActiveWorkbook.SaveAs "C:\Documents and Settings\" & Environ("username") & _
"\Desktop\" & "Regnskab " & Format(Sheets("Kasserapport").Range("A4"), "mm-yyyy") & _
" " & Sheets("Kasserapport").Range("D2").Text & ".xls"
MsgBox "Der er ikke forbindelse til G:-drevet." + Chr(10) + _
"Regnskabet er gemt p? skrivebordet.", vbExclamation, ""
End Sub
http://www.vbaexpress.com/forum/showthread.php?p=143854#post143854
But in some instances it do not function as required, and now i need to redirect both 'File -> Save', 'File -> SaveAS' and the icon 'Save' in Standard Toolbar to a sub Test_Navn if macros is enabled.
This is Workbook_BeforeSave code
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
Rem if SaveAs is in progress
Application.EnableEvents = False: Rem prevent Test_Navn from triggering cascading BeforeSave's
Call Test_Navn: Rem custom save routine
Cancel = True: Rem spike normal SaveAs
Application.EnableEvents = True
End If
End Sub
And the following 7 Sub's and Functions is all code related to save.
This is a sub and a function to test if conditions is meet in 2 cells in sheet "Kasserapport"
Sub Test_Navn()
Dim ws As Worksheet
Set ws = Sheets("Kasserapport")
If Test(ws) = False Then
Call Test_G
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
This is a sub and a function, called by the sub 'Test_Navn'. They test if drive G: is present and ready.
Sub Test_G()
If DExist("g") = 2 Then
Call G_Exist
End If
If DExist("g") = 0 Then
Call G_Do_Not_Exist
End If
End Sub
Public Function DExist(OrigFile As String)
Dim fs, d
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.driveexists(OrigFile) = True Then
Set d = fs.getdrive(OrigFile)
DExist = 1
If d.isready = True Then
DExist = 2
Exit Function
End If
Else
DExists = 0
End If
End Function
This is a sub and function called if drive G: is present and ready.
Sub G_Exist()
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
MsgBox "Regnskabet er gemt" + Chr(10) + _
"i beboerens regnskabsmappe p? G:-drevet"
End Sub
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
This is a sub called if drive G: is not present and ready.
Sub G_Do_Not_Exist()
ActiveWorkbook.SaveAs "C:\Documents and Settings\" & Environ("username") & _
"\Desktop\" & "Regnskab " & Format(Sheets("Kasserapport").Range("A4"), "mm-yyyy") & _
" " & Sheets("Kasserapport").Range("D2").Text & ".xls"
MsgBox "Der er ikke forbindelse til G:-drevet." + Chr(10) + _
"Regnskabet er gemt p? skrivebordet.", vbExclamation, ""
End Sub