[ Pobierz całość w formacie PDF ]
.Value.ToShortDateString & _ And [SentOn] 0 ThenDim strFolderKey As StringDim subFolder As Outlook.MAPIFoldersubFolder = subfolders.GetFirstWhile Not subFolder Is Nothingwww.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c10.qxd 11/11/01 4:17 PM Page 475PROGRAMMING OUTLOOK 475newNode = parentNode.Nodes.Add(subFolder.Name)newNode.Tag = subFolder.EntryIDScanSubFolders(subFolder, newNode)subFolder = subfolders.GetNextEnd WhileEnd IfEnd SubViewing a Folder s ContactsAfter populating the TreeView control with the structure of the subfolders under the Contactsfolder, you can select a folder in the TreeView control with the mouse to display its contacts on theListBox control at the right side of the form.When an item in the TreeView control is clicked, theAfterSelect event is triggered; the code for this is presented in Listing 10.20.This event reports thenode clicked, and you can use the event s argument to retrieve the node s tag, which is the ID of theselected folder.Once you know the ID of the selected folder, you can create a reference to this folder(variable selFolder) and use it to scan the contact items in the actual folder.Listing 10.20: Listing the Items of the Selected FolderPrivate Sub TreeView1_AfterSelect(ByVal sender As System.Object, _ByVal e As System.Windows.Forms.TreeViewEventArgs) _Handles TreeView1.AfterSelectIf e.Node.Tag Is Nothing Then Exit SubDim folderid As String = e.Node.TagDim selFolder As Outlook.MAPIFolderselFolder = OlObjects.GetFolderFromID(folderid)Dim itm As IntegerListBox1.Items.Clear()For itm = 1 To selFolder.Items.CountListBox1.Items.Add(selFolder.Items.Item(itm).Email1Address)ListBox1.Items.Add(vbTab & selFolder.Items.Item(itm).FullName)NextEnd SubThe code displays only the contact s name and e-mail address.You can modify the code to dis-play any fields.For example, you can retrieve the contact s e-mail address and send a message to allthe contacts in a specific folder, as we ll do in the last example of the chapter.Automated MessagesThe Send Message to Selected Contacts button demonstrates how to send a message once the userhas picked one or more recipients.The message s subject and body are hard-coded in this project,but you can easily modify the application so that it reads the message s body from a text file.Youwww.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c10.qxd 11/11/01 4:17 PM Page 476476 Chapter 10 AUTOMATING MICROSOFT OFFICE APPLICATIONScan also embed keywords into the text file and replace them with the recipient s name, title, address,and so on.The code (Listing 10.21) is short and straightforward, and it s meant to demonstrate thebasic steps for automating the creation and dispatch of electronic messages, placing them in the Out-box folder.From there, the message will leave in the same way as the messages you create manuallywith Outlook.Listing 10.21: Listing the Items of the Selected FolderPrivate Sub bttnSend_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles bttnSend.ClickDim msg As Outlook.MailItemmsg = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)Dim iContact As IntegerFor iContact = 0 To ListBox1.SelectedIndices.Count - 1msg.Recipients.Add(ListBox1.Items(_ListBox1.SelectedIndices.Item(iContact).ToString)msg.Subject = Automated Messagemsg.Body = Enter the message s body heremsg.send()NextEnd SubFirst, you must create a new MailItem object, variable msg, with the CreateItem method of Out-look.Then you set the fields of the MailItem object.There are many more properties you can set;they will all appear as soon as you enter the name of the variable that represents the MailItem objectand the following period.Finally, call the MailItem object s Send method to place it in the Outboxfolder, from which it will be sent the next time Outlook sends and receives messages.SummaryIf you add references to the various Office applications in your projects and then open the ObjectBrowser, you ll realize that Word, Excel, and Outlook expose many objects, which in turn, providenumerous properties and methods.Automating Office applications is well within the reach of theaverage Visual Basic programmer, as long as you familiarize yourself with the objects exposed bythese applications (or any other application that exposes an object model).www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c11.qxd 11/11/01 4:18 PM Page 477Part IIIBasic FrameworkClassesIn this section:Chapter 11: Storing Data in CollectionsChapter 12: Handling Strings, Characters, and DatesChapter 13: Working with Folders and Fileswww.sybex
[ Pobierz całość w formacie PDF ]