SaveAs with default Filename from cell, And setting file type

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:
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...
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Ok, so this works, other than that it crashes Excel:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.Dialogs(xlDialogSaveAs).Show ActiveSheet.Range("C2").Value, 52
End Sub

It seems that Excel is trying to run the macro twice... maybe the first time is on save, and the second time is on the save that is instigated by the macro itself. The crash happens AFTER the worksheet is correctly saved, so it would seem that this second run is what kills it. The question then becomes "how do I stop the macro from running twice?".

Of course, this train of thought may be totally wrong, in which case the question is "Why does it crash and how do I stop it from doing so?"
 
Upvote 0
More Info I used F8 to check the steps this ran:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)   '1
Dim DefaultName As String                                                       '2
DefaultName = ActiveSheet.Range("C2").Value                                     '3
SaveMe DefaultName                                                              '4
Exit Sub                                                                        '5
End Sub                                                                         '6

Sub SaveMe(DefaultName As String)                                               '7
    Application.Dialogs(xlDialogSaveAs).Show DefaultName, 52                    '8
End Sub

The sequence of lines and actions was as follows, when the macro ran:
1
3
4
-----
7
8
-----
1
3
4
-----
7
8
9
-----
5
Dialog box appears
Click save in the dialog
-----
9
-----
5
then crash


Hopefully this may help...
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top