The following VBA code will rename the file in the Workbook_Open event, using a value in a particular cell. In this example, the value is placed in cell A1 for simplicity.
Private Sub Workbook_Open()
ThisWorkbook.SaveAs Filename:=Range("A1").Value
End Sub
If you want to generate the filename at runtime, you can dynamically populate the cell which will referenced by the macro.
For example, if you are using the ExcelTemplate object you can place a data marker such as %%=$filename into a hidden cell in your template spreadsheet. Then, at runtime, dynamically generate the correct filename and bind the string to the datamarker, using the setCellDataSource() method.
Note: Other possible workarounds for the issue of the browser not preserving the filename correctly are:
- Save the file temporarily on the server and redirect the client to the actual file.
- Use OfficeWriter assistant on the client to open the file with the desired name.
For more information about using the OfficeWriter assistant, see the OfficeWriter documentation.
|