Home > How-To Library > MS Office

Outlook Automation

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
Sub Main() ' Start Outlook. ' If it is already running, you'll use the same instance... Dim olApp As Outlook.Application Set olApp = CreateObject("Outlook.Application") ' Logon. Doesn't hurt if you are already running and logged on... Dim olNs As Outlook.NameSpace Set olNs = olApp.GetNamespace("MAPI") olNs.Logon ' Create and Open a new contact. Dim olItem As Outlook.ContactItem Set olItem = olApp.CreateItem(olContactItem) ' Setup Contact information... With olItem .FullName = "Test Kie" .Birthday = "9/15/1945" .CompanyName = "ABC Co." .HomeTelephoneNumber = "123-315-1245" .Email1Address = "Aseer@ace.com" .JobTitle = "Web Designer" .HomeAddress = "5-a I Ave" & vbCrLf & "Tes Park, AY 17242" End With ' Save Contact... olItem.Save ' Create a new appointment. Dim olAppt As Outlook.AppointmentItem Set olAppt = olApp.CreateItem(olAppointmentItem) ' Set start time for 2-minutes from now... olAppt.Start = Now() + (2# / 24# / 60#) ' Setup other appointment information... With olAppt .Duration = 60 .Subject = "Discussion to discuss plans..." .Body = "Discussion with " & olItem.FullName & " to discuss plans." .Location = "Home Office" .ReminderMinutesBeforeStart = 1 .ReminderSet = True End With ' Save Appointment... olAppt.Save ' Send a message to your new contact. Dim olMail As Outlook.MailItem Set olMail = olApp.CreateItem(olMailItem) ' Fill out & send message... olMail.To = olItem.Email1Address olMail.Subject = "About our Discussion..." olMail.Body = "Dear " & olItem.FirstName & ", " & vbCr & vbCr & vbTab & _ "I will call you in 15 minutes for our Discussions!" & _ vbCr & vbCr & "Btw: I've also added you to my contact list." olMail.Send ' Clean up... MsgBox "Done...", vbMsgBoxSetForeground olNs.Logoff Set olNs = Nothing Set olMail = Nothing Set olAppt = Nothing Set olItem = Nothing Set olApp = Nothing End Sub

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.