Simple email templates in MS Outlook VBA

Small prescription for MS VB for system administrators who want to simplify the life of employees.

Task: the girl at the reception periodically sends official announcements to the entire company. In this design, drawn by the designer, and the sending process is down to "find an old letter sent, tap forward all, edit the text and the subject line of the email to send."

There are two ways:
Templates — suitable for simple options when all fields are static
VBA citizens consider the example of creating a simple pattern on the built-in Outlook Visual Basic.

There are of course the normal way is to take the programmer, who will write automatic newsletter emails to any convenient engine to obtain employee data from Exchange or LDAP and everything will be OK, but you need the budget. Anyone interested in crutch — a cat.

The VBA macros can be configured to auto-fill all the fields in a flexible way. Macro calls can be added directly into the Outlook ribbon, creating a symbol.
The user may accidentally delete a file from a template, or to bungle it. In such cases, the macro more reliable, and always on the quick buttons.

Layout of a letter in Outlook is in HTML, and below is the instruction and example of a simple code that creates a letter attached two images with logos, formats the html body of the message, registers the fields of the letter.

So:
the
    the
  • Launch the macro editor in Outlook (Alt+F11)
  • the
  • Create a new module
    image
  • the
  • Write code
    the
    Attribute VB_Name = "Announce"
    Option Explicit
    Sub CreateAnnounce()
    Dim htmlBody As String
    Dim fileAttach As Outlook.Attachment
    
    Dim newMail As Outlook.MailItem
    Set newMail = Application.CreateItem(olMailItem)
    
    'Attack two files that the html body can be referenced as src="cid:<file name>"
    'The third parameter (position) is set to 0 to attache were not displayed as  separate  files and does not spoil appearance
    Set fileAttach = newMail.Attachments.Add("c:\images\logo1.png", , 0)
    Set fileAttach = newMail.Attachments.Add("c:\images\logo2.png", , 0)
    Set fileAttach = Nothing // free the memory
    
    'forming the body of the email. Table, two rows in the upper two pictures at the bottom of the text. Some style and formatting.
    htmlBody = "<table align=center style='width:650;border:solid #316AA5 6.0 pt;background=white;padding:0'>" + _
    "<tr height=150>" + _
    "<td style='padding:20;width:200' valign=top><img src='cid:logo1.png' height=150></td>" + _
    "<td style='padding:20;width:200' align=right valign=top><img src='cid:logo2.png' height=150></td></tr>" + _
    "<tr><td colspan=2 valign=top style='padding:80'> <h3>Dear colleagues, </h3><br><br>" + _
    "SCIgen is a computer program that generates random text that resembles a scientific article, containing illustrations, graphics and notes. The  stated  purpose: "to automatically generate abstracts for conferences, suspected low qualification receive."" + _
    "<br><br>Thank you for your attention.<br><br><hr>With respect, the service of labour protection<br><br></tr></table>"
    
    With newMail
    .Subject = "[Announce] " + Format(Date, "dd.mm.yyyy, dddd")
    .To = "test@example.com"
    .BodyFormat = olFormatHTML
    .htmlBody = htmlBody
    .Display
    End With
    Set newMail = Nothing
    End Sub
    
  • the
  • 4. Add a call to the macro in the Outlook ribbon
  • the
  • 5. Profit
    image


How can you improve?
You can dobavit a few inputbox or a form with radio button where you can choose some parameters in choosing the recipients or, or need a subspecies of the announcement.
You can do a search in the outlook address book (Outlook cycle.Application.In this case("MAPI").AddressLists("Global Address List").AddressEntries), and use the information from the card (name, project, contact information), creating a personalized letter.
You can make some presets for different fields, and select randomly or by date/time.

Unfortunately, faced with the fact that if you view the html mail in desktop outlook, you may experience problems

Even the native web-interface exchange for some reason can bite off pieces that he deem unnecessary (I have it not display the pictures in the header of the table, although they display the pictures of employees in the same table). To find out the principle on which he chooses what to display or not, I couldn't — all the files attachfiles the same.
The reason is that exchange alters the entire html code and displays it according to their own rules, using cpan is a terrible fact that dizinil I (div/table...). Attempts to Google a solution yet sad — apparently in the current exchange it can be done. If you have had a successful solution — share!

PS Yes, it's certainly a crutch, but in my company it has saved approximately 200 man-hours per year, which is a lot.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Car navigation in detail

PostgreSQL: Analytics for DBA

Google has launched an online training course advanced search