Page 479 - Microsoft Office Excel 2003 Programming Inside Out
P. 479

Excel and Other Office Applications
























                             Figure 21-3.  The References dialog box displays all available references. To activate a
                             library file, select the reference and click OK.
                                                                                                             Chapter 21
                             Now that the reference has been added to the Visual Basic Editor, you’ll have the available
                             assistance with objects created using this library file. VBA will search through the type librar
                             ies, in the order shown from top down, to find the references to object types. If the same
                             object type is present in more than one library, VBA will use the first one found. You can
                             select a library and click the Priority buttons to move it up and down the list to change the
                             order in which libraries are searched. There’s no need to depend on priority; you can refer
                             ence the object with the application object. For example, instead of using AddressList, use
                             Outlook.AddressList.
                             Review the following example to see how early binding is used. The procedure lists all the
                             names in the Outlook Contacts folder, placing them in column A of the active worksheet.
                             Make sure that you have added a reference to the Outlook Object Library before executing
                             this procedure.

                             Sub DisplayOutlookContactNamesEarlyBinding()
                                 Dim olApp As Outlook.Application
                                 Dim olNameSpace As Outlook.Namespace
                                 Dim olAddresslist As AddressList
                                 Dim olEntry As AddressEntry
                                 Dim i As Long
                                 Set olApp = New Outlook.Application
                                 Set olNameSpace = olApp.GetNamespace("Mapi”)
                                 Set olAddresslist = olNameSpace.AddressLists("Contacts”)
                                 For Each olEntry In olAddresslist.AddressEntries
                                    i=i+1
                                    Cells(i, 1).Value = olEntry.Name
                                 Next

                                 olApp.Quit
                                 Set olApp = Nothing
                             End Sub
                                                                                                       453
                                                                        Part 6:  Excel and the Outside World: Collaborating Made Easy
   474   475   476   477   478   479   480   481   482   483   484