Microsoft Excel is a multiple document interface (MDI) application. This means that you can open many workbooks at the same time and be limited only by the memory on your computer. For this reason, the ability to programmatically open a workbook is handled by the Workbooks collection. To support this, the Workbooks class is equipped with a method named Open. Its syntax is: Workbooks.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad) FileName is the only required argument. When calling this method, you must provide the name of the file or its path. This means that you can provide a file name with its extension. Here is an example: Private Sub cmdOpenWorkbook_Click() Workbooks.Open "SchoolRecords.xlsx" End Sub If you provide only the name of a file, Microsoft Excel would look for it in the current directory or in My Documents (Documents in Windows Vista). If Microsoft Excel cannot file the file, you would receive an error: As you can imagine, a better alternative is to provide a complete path to the file.
After using a workbook or to dismiss a document you don't need, you can close it. To support this operation, the Workbook class is equipped with a method named Close. Its syntax is: Public Sub Close(Optional ByVal SaveChanges As Boolean, Optional ByVal Filename As String, Optional ByVal RouteWorkbook As Boolean) All three arguments are optional. The first argument indicates whether you want to save the changes, if any have been made on the workbook since it was opened. If no change had been made since the time the workbook was created or since the last time it was opened, this argument is not considered. If the first argument is set to True and the workbook has changes that need to be save, the second argument specifies the name of the file to save the workbook to. The third argument specifies whether the workbook should be sent to the next user.
If you have many workbooks you don't need, you can close all of them. To support this operation, the Workbooks collection class is equipped with a method named Close. Its syntax is: Public Sub Workbooks.Close() This method takes no argument. When called, it closes all workbooks that are currently opened in Microsoft Excel.
Microsoft Excel is a multiple document interface (MDI). This means that the application allows you to switch from one workbook to another, or be able to display all of them sharing the same screen. When many workbooks have been opened in, to display many of them, you can arrange them in:
To access a workbook, the Workbook class is equipped with a method named Activate. Its syntax is: Workbook.Activate() This method takes no argument. Therefore, to call it, you can get a reference to the workbook you want to access, then call the Activate() method. Here is an example: Private Sub cmdSelectWorkbook_Click() Dim SchoolRecords As Workbook Set SchoolRecords = Workbooks.Item(2) SchoolRecords.Activate End Sub You can also do this with less code by applying the index directly to the Workbooks collection. Here is an example: Private Sub cmdSelectWorkbook_Click() Workbooks(2).Activate End Sub If you create or open many workbooks and while you are working on them, each is represented on the taskbar by a button. To programmatically refer to a workbook, access the Item property and pass either the index or the file name of the workbook to it. Here is an example: Private Sub cmdSelectWorkbook_Click() Workbooks.Item (2) End Sub After referring to a workbook, if you want to perform an action on it, you must get a reference to it. To do this, declare a Workbook variable and assign the calling Item() to it. This would be done as follows: Private Sub cmdSelectWorkbook_Click() Dim SchoolRecords As Workbook Set SchoolRecords = Workbooks.Item(2) End Sub |
|
|||||||||||||||||||||||||
|
|
||
Previous | Copyright © 2007-2010, FunctionX | Next |
|