ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ActiveWorkbook.SaveAs does not work (https://www.excelbanter.com/excel-programming/401903-activeworkbook-saveas-does-not-work.html)

Cooz

ActiveWorkbook.SaveAs does not work
 
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:=str FileName)
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


All times are GMT +1. The time now is 11:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com