Access VBA Macro Programming

(Joao Candeias) #1
The first four lines set up variables based on the Microsoft Outlook types. These are for
the Outlook application, the NameSpace, the Outlook folder, and the Mail item. The variable
MoOutlookis set to point to the namespace MAPI. This represents one of the messaging
service provider layers that Outlook depends on for data storage. MAPI is the only type of
namespace that Outlook supports.
The variableoFolderis then set to the default folder for the Outbox for the namespace.
This sets up an object that represents the Outbox, into which you can then place your mail
item. You do this by setting the variableoItemto a new mail item within that folder. This is
exactly the same as when you open a new mail item in Microsoft Outlook itself. The code
then goes through all the stages it normally does from the front end of Microsoft Outlook to
create your e-mail.
The address of the recipient is added. At this point, you can add in your own address as a
string to try out the example. If the recipient is internal to your network and is on the Outlook
address list, you can simply use the name. The subject is what you would see in the title for
the message. The body is the e-mail text itself. Don’t forget you can use Chr(13), Chr(10), or
vbCrLf to provide carriage returns in a string so you can insert a “Regards” statement at the end.
Importance allows you to set what priority you want the e-mail to have. The Importance
constants are shown in a list box as you type this statement in. They are as follows:

olImportanceHigh
olImportanceNormal
olImportanceLow

Send is the actual sending of the e-mail. Once the e-mail has been sent, the variables
oItemandoFolderare set to Nothing, and the memory is released.
Note that this uses the same technology as many e-mail viruses do, most notably the
infamous Love Bug virus. You need to be careful with this code because if you’re not, it can
end up sending a lot of automated e-mails, which can clog up the mail system and make for
angry recipients.
Microsoft has added security to later versions of Outlook, so, although this code will still
work very well, the user will get a message box advising that e-mail is about to be sent. In
any instructions to users you provide, you need to point out that this will happen when they
take your e-mail feature, and they need to okay the e-mail being sent to them.

Driving Access from Other Office Programs


In the previous sections, you saw how Access can be used to drive other Microsoft
applications such as Word or Outlook.
By the same token, these other applications can also be used to control Access. For
example, you can write a macro in Word or Excel that populates tables in an Access
database. You can even execute queries providing that they do not return data (as in a Select
query). Access does not actually have to be running, and the database itself does not have to
be seen on screen, although if the database is already open it will not cause problems because
it is a multiuser application.

226 Microsoft Access 2010 VBA Macro Programming

Free download pdf