I have created a form that I would like to email to multiple users. I would like them to complete the form, save it with a new (unique) file name, and email it back to me.
I am using Excel 2007.
How can I automate the process of saving the file with a name found in a single given cell?
Note: I am new to macros and visual basic and will require very explicit instruction. Thanks in advance for your help and patience.
To save the workbook:
ThisWorkbook.SaveAs Filename:=Sheets("Sheet1").Range("A1")
To email the workbook:
ThisWorkbook.SendMail Recipients:="your@email.com", Subject:="Emailed Workbook"
Thank you for your help. It is the saving (your first code) that I need to automate.
How will the form user activate the code to save? Do I need to provide instructions?
You could put that line in a Sub in a Module and then assign it to a Form button on the sheet. Or you could use the workbook BeforeSave event. That way any time the users clicks the save icon, it would automatically save the file with the correct filename. This would lead to issues where, if a file with that name already exists, it would ask the user if they want to override the existing file. This can be prevented with
Application.DisplayAlerts = False
Last edited by Whizbang; 08-15-2011 at 04:05 PM.
Sorry... real newbie here...
Can you explain "BeforeSave event"?
Here is the codePrivate Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Application.EnableEvents = False ' We turn off event because otherwise we would get caught in a loop of save events. Application.DisplayAlerts = False ' This prevents the dialog box opening and asking if the user wants to overwrite an existing file. ThisWorkbook.SaveAs Filename:=Sheets("Sheet1").Range("A1") ' Save the file with the appropriate name. Application.DisplayAlerts = True ' Turn the alerts back on. Application.EnableEvents = True ' Turn events back on Cancel = True ' We cancel the save event because we already saved in the lines above. End Sub
Please see the attached workbook as an example.
To view the code, right-click on any sheet and select "View Code". This will take you to the VBA editor. On the left you should see a navigation pane listing all open workbooks. If your workbook is not already expanded (it should be) then expand it using the + symbol to the left of the workbook name. The double-click on the "ThisWorkbook" module to display the code.
For more information on the VBA editor and modules, do a Google search for VBA basics.
Essentially, each sheet gets its own module. The workbook as a whole gets its own module as well. Each of these modules have pre-set "events" than can trigger custom code. Other events at the workbook level include the Open event, Close, BeforePrint, NewSheet, SheetActivate, etc. Each of these events are tirggered when the user performs an action, such as clicking the "Save" button, or navigating between sheets, or closing the workbook.
Last edited by Whizbang; 08-15-2011 at 04:19 PM.
Thank you!
I see how it works in your sample sheet and will try applying it to mine.
Ran into a glitch. Got this message when clicking Save:
The following features cannot be saved in macro-free workbooks:
VB project
To save a file with these features, click No, and then choose a macro-enabled file type in the File Type list.
Nevermind. I seem to have solved the problem by saving it in Excel 97-2003 format.
Try this:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Application.EnableEvents = False ' We turn off event because otherwise we would get caught in a loop of save events. Application.DisplayAlerts = False ' This prevents the dialog box opening and asking if the user wants to overwrite an existing file. ThisWorkbook.SaveAs Filename:=Sheets("Sheet1").Range("A1"), FileFormat:=52 ' Save the file with the appropriate name. Application.DisplayAlerts = True ' Turn the alerts back on. Application.EnableEvents = True ' Turn events back on Cancel = True ' We cancel the save event because we already saved in the lines above. End Sub
For a explaination on the FileFormat number and what it means, see this link: http://www.rondebruin.nl/saveas.htm
Last edited by Whizbang; 08-16-2011 at 11:04 AM.
Excellent resource. You are teaching me a lot.
Since I don't know what version of Excel my email recipients/worksheet users will be using, it appears file format=52 (as in your code) is the safest.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks