Page 62 - Excel Progamming Weekend Crash Course
P. 62

d540629 ch03.qxd  9/2/03  9:27 AM  Page 37




                  Session 3 — The Excel Object Model                                      37

                          Your code should always take possible errors into account, particularly when
                          working with files. Sample code in this session and others usually omits the
                   Tip    error-handling code for the sake of clarity, but that does not mean you can
                          do the same!


               Saving and Closing Workbooks

               To save a workbook under its existing name, use the Save method. A workbook’s name is
               either the name you assign using the SaveAs method (described later in this session) or the
               default name (Book1, Book2, and so on) that Excel assigns when a new workbook is created.
               The Save method takes no arguments.
                  To save a workbook under a new name, use the SaveAs method:
                  WB.SaveAs(Filename)
                  WB is a reference to the workbook, and filename is the name under which to save it. The
               name can include the .XLS extension or not; if this is omitted, Excel automatically adds it.
               The name can also include drive and/or path information if you do not want the workbook
               saved in Excel’s default data folder. For example:

                  MyWB.SaveAs(“g:\data\sales\march.xls”)

                  The SaveAs method has some additional, optional arguments that are used to assign a
               password to a workbook, specify the backup mode, and set other save options. Please refer
               to VBA’s online help for the details.
                  Another method, SaveCopyAs, enables you to save a copy of a workbook under a new
               name without changing the name of the open workbook. The syntax is:

                  WB.SaveCopyAs(Filename)
                  To close an open workbook, use the Close method, as shown in the following syntax:

                  WB.Close(SaveChanges, Filename, RouteWorkbook)
                  Here’s what each of these optional arguments do:

                   SaveChanges. Set this argument to True to save changes made since the workbook
                   was last saved, or False to discard such changes. If omitted, the user is prompted to
                   save changes.
                   Filename. This argument designates the name to save the workbook under. If omit-
                   ted, the current workbook name is used. If the workbook has not been assigned a
                   name (it’s still using the default name Book1, Book2, and so on) and this argument
                   is omitted, the user is prompted for a name.
                   RouteWorkbook. This argument is relevant only if a routing slip is attached to the
                   workbook and it has not already been routed. Set to True to route the workbook, or
                   False to not route it. If this argument is omitted and a routing slip is attached, the
                   user is prompted.
   57   58   59   60   61   62   63   64   65   66   67