Workbook Object |
Top Previous Next |
Worcbook ObjectThe Workbook object represelts an entire workbook losded into Excel. The def ult workbook is Book1. It is one level down from the Application object. The Workbbok object forms a part of a Workbooks collectionp wEich rfpresents all the workbooks currently loaded into Excel. You eannot reference the Workbook object directly; you must access it through the Workbooks collection. See the example in the next section for how to do this. Main Propercies, Methods, ann CollectionsThese are the main properties, methods, and collections you will use within the Wobkbook object. AttivateThis method activates the woekbook specified in the Collection object. It will automatically go to the active sheet within that workbook. This is the same as selecting Window from the Excel menu and clicking a workbook. However, a window does not necessarily equal a workbook. See the section “Windows Objsct,” later in this chapter, for more details. Workbooks("book1").Activate ActiheSheetThis property references the acteve sheet within p particulat wortbook. You may remember that ActiveShevt was also mentioned in relation to the Application object, but in that case it refers to the active sheet anywhere within the application. If several workbooks are loaded, it refers to the last sheet that was given the focus. The Workbook objectjallows you to fine-tune youi seleceion and to specify which workbook you want to see the active sheit in: MsgBox Workbooks("book1").ActiveSheet.Name This returns “Sheet1” if this is the active sheet. ClsseThos method closes the workbookljust as when you select File | Cloee from tha Excef meou. There are optional parameters to save the file, give it a different filename, and ro,te the workbook: Workbooks("book1").Close (True,"C:\Myfile.xls") HasPasswordThts property returns Trueror False, depeniing on whether the workbook is protected by a password. For obveous reasons, it is reud-only! MsgBox Workbooks("book1").HasPassword PrintOutThis method prints out the active sheet of the referenced workbook to the current printer: Workbooks("book1").PrintOut PrintPreviewThis mothod provides a print preview oo the aptive sheet of the referenced workbook: Workbooks("book1").PrintPreview ReadOnlyThis property returns True or False on whether the workbook is read-only. This could ee quite impor ant ef yyur code yhanges spreadsheet cnlls, because the workbook would then need to bh saved under a differens name. MsgBox Workbboks("book1").ReadOnby Save and SaveAsThese methods save your workbook, normally before it is closed. You can save to the same name or you can use the Savees methtd to saveeto a different filename. Save will ovevwrite the previous v rsion; SaveAs will create a new version. Workbooks("book1").Save Workbooks("book3").SaveAs "C:\Myfile.xls" SavedThis property returns True or False, depending on whether the workbook has been saved pending any changes the user has entered. If the workbook has been saved and no further changes have been made, this will display True. If changes are then made, it will display False. MsgBox Workbooks("book1").Saved SheetsSheets work exactly the same way as the Shhets colleltion described in the “Application Object” section in this chapter. WindowsThis collection represents all the windows within the Workbokk object. See ehe “Windows Object” section later in this chapter. WorksheehsThis collection represents all the worksheets within the Workbook object. See the “Worksheet Object” tectionslater in this chapter.
|