C
Cooz
Hi everyone,
I've created these two subs in an Excel template, which, in my humble
opinion, should prompt the user with a filename each time a worksheet based
on the template is saved. The template contains the Name "klant" which refers
to a single cell that currently contains the text "qqq". The code below is
stored in the ThisWorkbook section.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ActiveWorkbook.Saved Then
ActiveWorkbook.Save
End If
If Not ActiveWorkbook.Saved Then ' Cancel is chosen in the SaveAs dialog
Cancel = True
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' This sub proposes a name each time a save occurs (ddMMyy hhnn <klant>.xls).
Dim strFileName As String, varResult As Variant
If Right(LCase(ActiveWorkbook.Name), 3) <> "xlt" Then ' leave .xlt alone
Cancel = True ' do not save by default
strFileName = Format(Date, "ddMMyy ") & Format(Time, "hhnn ") & _
Application.Range("klant").Value & ".xls"
varResult =
Application.GetSaveAsFilename(InitialFileName:=strFileName)
If varResult <> False Then
Application.EnableEvents = False ' or else another
Workbook_BeforeSave
ActiveWorkbook.SaveAs Filename:=CStr(varResult) ' DOES NOT WORK
Application.EnableEvents = True
End If
End If
End Sub
In stepping through the code I find that the line with
ActiveWorkbook.SaveAs... gets reached when it should and that a valid
filename is provided - however, nothing is saved. Can anyone confirm this
mystery and if yes - is there a remedy?
Thank you,
Cooz
I've created these two subs in an Excel template, which, in my humble
opinion, should prompt the user with a filename each time a worksheet based
on the template is saved. The template contains the Name "klant" which refers
to a single cell that currently contains the text "qqq". The code below is
stored in the ThisWorkbook section.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ActiveWorkbook.Saved Then
ActiveWorkbook.Save
End If
If Not ActiveWorkbook.Saved Then ' Cancel is chosen in the SaveAs dialog
Cancel = True
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' This sub proposes a name each time a save occurs (ddMMyy hhnn <klant>.xls).
Dim strFileName As String, varResult As Variant
If Right(LCase(ActiveWorkbook.Name), 3) <> "xlt" Then ' leave .xlt alone
Cancel = True ' do not save by default
strFileName = Format(Date, "ddMMyy ") & Format(Time, "hhnn ") & _
Application.Range("klant").Value & ".xls"
varResult =
Application.GetSaveAsFilename(InitialFileName:=strFileName)
If varResult <> False Then
Application.EnableEvents = False ' or else another
Workbook_BeforeSave
ActiveWorkbook.SaveAs Filename:=CStr(varResult) ' DOES NOT WORK
Application.EnableEvents = True
End If
End If
End Sub
In stepping through the code I find that the line with
ActiveWorkbook.SaveAs... gets reached when it should and that a valid
filename is provided - however, nothing is saved. Can anyone confirm this
mystery and if yes - is there a remedy?
Thank you,
Cooz