So you think you’re ready to play around with messages eh? Well, true, you probably are. Messages are referred to as ‘items’ in Outlook, as you can also have appointments & notes etc. For now, we’ll be dealing with the items located within the Inbox folder.
The next snippet of code shows you how to simple create a message, write a short message, and save it in the Drafts folder:
Private Sub Form_Load()
Dim objOutlook As New Outlook.Application Dim objNameSpace As Outlook.NameSpace Dim objInbox As MAPIFolder Dim objMail As MailItem
注释:Get the MAPI reference Set objNameSpace = objOutlook.GetNamespace("MAPI")
注释:Pick up the Inbox Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objMail = objInbox.Items.Add With objMail .Subject = "Well done" .Body = "This article is great!" .To = "sam@vbsquare.com" .Save End With End Sub
If you look closely at the code you’ll see that I’ve been rather cheeky with the body and subject lines, so to get your own back you can run the next piece of code to delete my ego booster!
Private Sub Form_Load()
Dim objOutlook As New Outlook.Application Dim objNameSpace As Outlook.NameSpace Dim objDrafts As MAPIFolder Dim objMail As MailItem
注释:Get the MAPI reference Set objNameSpace = objOutlook.GetNamespace("MAPI")
注释:Pick up the Drafts folder Set objDrafts = objNameSpace.GetDefaultFolder(olFolderDrafts)
Set objMail = objDrafts.Items("Well done") objMail.Delete End Sub
Right, that’s just about it for the first part in this series! In part 2 you’ll learn how to:
Filter emails as you read them in through different criteria Build a program which allows you to: Read in messages from a folder Filter duplicates & blanks Apply a date criteria Locate the email details Write them to a file See you again soon for some more exciting Outlook programming!