Follow by Email

Tuesday, March 18, 2014

Basic examples for Outlook VBA

This post contains few basic examples for VBA in Microsoft Outlook. To get developer tab visible, File -> Option -> Customize Ribbon -> Developer.

Let see a simple example to send an email. Below is the basic code which will generate and shoot the email. In visual Basic Editor, use ThisOutlookSession or any other user modules.

Private Sub SendEmail()
Dim olObj, mlObj
Set olObj = CreateObject("Outlook.Application") 'declaring object
Set mlObj = olObj.CreateItem(0) 'declaring an email item
With mlObj
.To = "me@mydomain.com" 'Give your email
.Subject = "Testmail" ' Give the subject
.Body = "Testmail" ' Give the Body
.Send 'command to send the email
End With
Set olObj = Nothing 'Clear the memory
Set mlObj = Nothing
End Sub


If you are asked for permission while before sending the email, then you can set the security level accordingly to avoid dialogboxes. Here is an another example for reading a subject from opened email item

Dim oMail As Outlook.MailItem 'declare item variable
Dim sSub As String
Set oInspector = Application.ActiveInspector 'declare an object
If oInspector Is Nothing Then ' This is to skip if no email is opened
MsgBox "No Mail is opened"
Exit Sub
End If
Set oMail = Application.ActiveInspector.CurrentItem 'assign the current email item
sSub = oMail.Subject 'Assign subject
sBob = oMail.Body ‘This will read Body
msgBox (sSub)

Using Regex pattern you can get any information from subject line and then process it accordingly. similarly, you can also forward the email item to any. But Outlook 'Rule' feature allows you to set directly forward, However below code is for your customization

Sub email_forward()
Set myitem = ActiveExplorer.Selection(1).Forward
myitem.Subject = Replace(myitem.Subject, "FW:", "")
myitem.To = "me@mydomain.com"
myitem.Save
myitem.Send
End Sub

1 comment:

  1. Silahkan di kunjungi ya kawan-kawan 100% Memuaskan
    > Hoki anda ada di sini <
    Allsbobet 1 DOMPET DEPOSITO Untuk Semua Permainan !!
    WELCOME BONUS 100%
    SPORTSBOOK CASINO TOGEL POKER E E-GAMES SLOT MACHINE NUMBER GAME SABUNG AYAM TEMBAK IKAN DAN SEBAGAINYA

    Tentunya dengan berbagai BONUS-BONUS:
    - Minimal Deposit Rp 50.000 & Withdraw Rp 50.000
    - BONUS NEW MEMBER SPORTBOOK 100%
    - BONUS ALL GAME 10%
    - BONUS ROLLINGAN LIVE CASINO 0.8%
    - BONUS REFFERAL 2.5%

    Gabung yuk bersama dengan kami di ALLSBOBET.COM
    Terpercaya Sejak 2015 - MENANG BERAPAPUN KAMI BAYAR
    Proses Deposit / Withdraw gak pake ribet, proses CEPAT !!

    UNTUK INFORMASI SELANJUTNYA BISA HUB KAMI DI :
    LIVECHAT ALLSBOBET.COM 24 JAM ONLINE : https://lc.chat/now/8533171/
    Pin BBM : D60E1BF8
    LINE : ALLSBOBET
    WECHAT : ALLSBOBET
    WHATSAPP : +855 974 832 357

    Agen Togel
    Agen Bola
    Agen Kasino
    Togel SGP
    Togel Hongkong
    Togel Sydney
    Judi Online
    Agen Judi
    Agen Judi QQ
    Forum Judi Online

    hubungi kami di :
    Line : ALLSBOBET
    Bbm : D60E1BF8
    Wa : +855974832357

    ReplyDelete

You Can use HTML code once it converted to NCcode by NCode converter