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.
To support the ability the programmatically 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
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
|
|
|||||||||
|