IN THE SPOTLIGHT: MDE to MDB Conversion Service
(also supports: ACCDE to ACCDB, ADE to ADP, etc)
IN THE SPOTLIGHT: Access Database Repair Service
An in-depth repair service for corrupt Microsoft Access files
IN THE SPOTLIGHT: vbWatchdog
VBA error handling just got easier...
" vbWatchdog is off the chart. It solves a long standing problem of how to consolidate error handling into one global location and avoid repetitious code within applications. "
- Joe Anderson,
Microsoft Access MVP
Meet Shady, the vbWatchdog mascot watching over your VBA code →
(courtesy of Crystal Long, Microsoft Access MVP)
IN THE SPOTLIGHT: vbMAPI
An Outlook / MAPI code library for VBA, .NET and C# projects
Get emails out to your customers reliably, and without hassle, every single time.
Use vbMAPI alongside Microsoft Outlook to add professional emailing capabilities to your projects.
IN THE SPOTLIGHT: Code Protector
Standard compilation to MDE/ACCDE format is flawed and reversible.
Let's dive in with an example of how to send a simple e-mail with vbMAPI:
Dim Session As vbMAPI_Session Dim Store As vbMAPI_Store Dim Folder As vbMAPI_Folder Dim FolderItems As vbMAPI_FolderItems Dim Item As vbMAPI_MailItem Set Session = vbMAPI_Init.NewSession Session.LogOn , , True Set Store = Session.Stores.DefaultStore Set Folder = Store.GetDefaultFolder(FolderType_Outbox) Set FolderItems = Folder.Items Set Item = FolderItems.Add Item.To_ = "some_email_address@mail.com" Item.Subject = "Some Subject" Item.HTMLBody = "<b>HTML message here...</b>" Item.Send Session.OutlookSendReceiveAll ' Required if not dealing with an Exchange account
Dim Session As vbMAPI_Session Dim Store As vbMAPI_Store Dim Folder As vbMAPI_Folder Dim FolderItems As vbMAPI_FolderItems Dim Item As vbMAPI_MailItem Session = vbMAPI_Init.NewSession Session.LogOn(, , True) Store = Session.Stores.DefaultStore Folder = Store.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox) FolderItems = Folder.Items Item = FolderItems.Add Item.To_ = "some_email_address@mail.com" Item.Subject = "Some Subject" Item.HTMLBody.Value = "<b>HTML message here...</b>" Item.Send Session.OutlookSendReceiveAll() ' Required if not dealing with an Exchange account
vbMAPI_Session Session; vbMAPI_Store Store; vbMAPI_Folder Folder; vbMAPI_FolderItems FolderItems; vbMAPI_MailItem Item; Session = vbMAPI_Init.NewSession(); Session.LogOn("", "", true, true, 0, false); Store = Session.Stores.DefaultStore; Folder = Store.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox); FolderItems = Folder.Items; Item = FolderItems.Add(null); Item.To_ = "some_email_address@mail.com"; Item.Subject = "Some Subject"; Item.HTMLBody.Value = "<b>HTML message here...</b>"; Item.Send(); Session.OutlookSendReceiveAll();
Description:
The root class of vbMAPI is called vbMAPI_Init - this is the starting point. We call the NewSession method which returns an object of type vbMAPI_Session.
We then need to log-on to a MAPI profile - we do this by using the Session.LogOn method. There are several optional parameters for the LogOn method (such as ProfileName, Password), but if you just want to log on to the default profile, then you can leave the parameters blank as shown here.
Next, we need to determine which MAPI store we are interested in. Stores identify the physical storage places for your e-mails. For example, if you have one Outlook PST file for your active e-mails and a separate one for your archived e-mails, then each of these is a "store", in MAPI terminology.
Usually you will only be interested in the default store (the one where new messages get delivered to). We get a pointer to the default store by calling the Session.Stores.DefaultStore method which returns an object of type vbMAPI_Store.
The vbMAPI_Store object provides access to all of the folders within a MAPI store. We use the vbMAPI_Store GetDefaultFolder method to obtain a reference to a standard folder within the MAPI store. Standard folders like "Inbox", "Drafts" and "Sent Items" (and more) are accessed in this manner. Intellisense will provide a full list for you.
The vbMAPI_Folder object provides access to messages and sub-folders within a MAPI folder. We then use the vbMAPI_Folder FolderItems method to obtain a reference to a collection of messages (type vbMAPI_FolderItems). Similarly we use the vbMAPI_Folder Folders method to obtain a reference to a collection of sub-folders type vbMAPI_Folders. In this particular example, we use the FolderItems method as we want to create a new mail item.
For this example, we then call the Add method of the vbMAPI_FolderItems object to create a new mail item (type vbMAPI_MailItem). With the mail item we then set some properties of the mail item, and eventually call the Send method which submits the message to the recipient.
You will note that the To method has a trailing underscore. This is due to VB/VBA not allowing "To" to be the name of a procedure (it sees it as a reserved word), so we had to improvise a little.
Naturally, the vbMAPI_MailItem object has all that you would expect to find from such an object. For example; a Recipients collection, an Attachments collection, and many properties similar to the Outlook.MailItem.
Dim Session As vbMAPI_Session Dim Item As vbMAPI_MailItem Set Session = vbMAPI_Init.NewSession Session.LogOn , , True Set Item = Session.GetDefaultFolder(FolderType_Outbox).Items.Add Item.To_ = "some_email_address@mail.com" Item.Subject = "Some Subject" Item.HTMLBody = "<b>HTML message here...</b>" Item.Send
Dim Session As vbMAPI_Session Dim Item As vbMAPI_MailItem Session = vbMAPI_Init.NewSession Session.LogOn(, , True) Item = Session.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox).Items.Add Item.To_ = "some_email_address@mail.com" Item.Subject = "Some Subject" Item.HTMLBody.Value = "<b>HTML message here...</b>" Item.Send
vbMAPI_Session Session; vbMAPI_MailItem Item; Session = vbMAPI_Init.NewSession(); Session.LogOn("", "", true, true, 0, false); Item = Session.GetDefaultFolder(EnumDefaultFolderType.FolderType_Outbox).Items.Add(null); Item.To_ = "some_email_address@mail.com"; Item.Subject = "Some Subject"; Item.HTMLBody.Value = "<b>HTML message here...</b>"; Item.Send();
Things to note:
To get further vbMAPI examples, please refer to the Code Examples document
iTech Masters | VAT: GB202994606 | Terms | Sitemap | Newsletter