Wednesday, November 2, 2011

Saving a Workbook

To visually save a workbook, you can click the Office Button and click Save. You can also press Ctrl + S. If the document was

saved already, it would be saved behind the scenes without your doing anything else.

To support the ability to programmatically save a workbook, the Workbook class is equipped with a method named Save. Its

syntax is:

Workbook.Save()

As you can see, this method takes no argument. If you click the Office Button and click Save or if you call the

Workbook.Save() method on a work that was not saved yet, you would be prompted to provide a name to the workbook.

To save a workbook to a different location, you can click the Office Button, position the mouse on Save As and select from

the presented options. You can also press F12. To assist you with programmatically saving a workbook, the Workbook class is

equipped with a method named SaveAs. Its syntax is:

Workbook.SaveAs(FileName,
FileFormat,
Password,
WriteResPassword,
ReadOnlyRecommended,
CreateBackup,
AccessMode,
ConflictResolution,
AddToMru,
TextCodepage,
TextVisualLayout,
Local)

The first argument is the only required one. It holds the name or path to the file. Therefore, you can provide only a name of

the file with extension when you call it. Here is an example:

Private Sub cmdNewWorkbook_Click()
Dim SchoolRecords As Workbook

Set SchoolRecords = Workbooks.Add

SchoolRecords.SaveAs "SchoolRecords.xlsx"
End Sub

If you provide only the name of a file when calling this method, the new workbook would be saved in the current directory or

in My Documents (Documents in Windows Vista). If you want, an alternative is to provide a complete path to the file.

No comments: