High Plains Grifter
Board Regular
- Joined
- Mar 9, 2010
- Messages
- 129
Hi all,
I am having a bit of a problem with a bit of VBA. The context is that I have made a template which users will use as a request form for new reports. This template contains a macro. I will email a link to this template to users, but I want to stop them from saving their requests over the template. To this end, I figured I would run a macro on save to show the XLDialogSaveAs, but I have not been able to set the type as '.xlsm' (FileFormat number 52). If the default format is used, the macros are obviously not saved.
I have tried all sorts of variations, but to no avail; Excel either ignores my attempts to control the format, puts inverted commas around the filename, crashes or does nothing. So I come to you. The latest iteration is as follows:
This works... In a way - Excel saves the file with the right name, in the right place and with the right extension, but I have lost the dialog box (It caused problems as well so I tried without it) so the user cannot change the name (I guess I could bring up an input box to get around this, but it is messy) and worst of all, Excel crashes every time I save. Excel 2010 automatically reopens my newly saved-as file, but crashes are not part of the plan!
Any ideas?
By the way, this one works as well... in a way
This one brings up the dialog, but the default filename is with the extension, in inverted commas. If I click OK, the file is saved with the right filetype, but again, Excel crashes. When I auto recover, I see a file called (for example) filename.xlsm.xlsm
Can't help thinking this should be easy...
I am having a bit of a problem with a bit of VBA. The context is that I have made a template which users will use as a request form for new reports. This template contains a macro. I will email a link to this template to users, but I want to stop them from saving their requests over the template. To this end, I figured I would run a macro on save to show the XLDialogSaveAs, but I have not been able to set the type as '.xlsm' (FileFormat number 52). If the default format is used, the macros are obviously not saved.
I have tried all sorts of variations, but to no avail; Excel either ignores my attempts to control the format, puts inverted commas around the filename, crashes or does nothing. So I come to you. The latest iteration is as follows:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sDirectory As String
Dim sFilename As String
Dim instance As XlFileFormat
instance = ActiveWorkbook.FileFormat
sFilename = ActiveSheet.Range("C2").Value
sDirectory = "P:\GoTrex\Configuration Live\User Change Requests\Reports\"
ActiveWorkbook.SaveAs Filename:=sDirectory & sFilename, FileFormat:=instance
End Sub
This works... In a way - Excel saves the file with the right name, in the right place and with the right extension, but I have lost the dialog box (It caused problems as well so I tried without it) so the user cannot change the name (I guess I could bring up an input box to get around this, but it is messy) and worst of all, Excel crashes every time I save. Excel 2010 automatically reopens my newly saved-as file, but crashes are not part of the plan!
Any ideas?
By the way, this one works as well... in a way
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Dialogs(xlDialogSaveAs).Show ("P:\GoTrex\Configuration Live\User Change Requests\Reports\" & ActiveSheet.Range("C2").Value & ".xslm")
End Sub
This one brings up the dialog, but the default filename is with the extension, in inverted commas. If I click OK, the file is saved with the right filetype, but again, Excel crashes. When I auto recover, I see a file called (for example) filename.xlsm.xlsm
Can't help thinking this should be easy...