Article ID: 170262
Article Last Modified on 1/20/2007
Sub SendMail()
Dim objOutlook As New Outlook.Application
Dim objMail As MailItem
Set objMail = objOutlook.CreateItem(olMailItem)'Create a new
'MailItem
With objMail
.To = "JohnDoe;JaneDoe" 'Recipients
.Subject = "Subject of mail message" 'Subject line of
'message
.Body = "Body of mail message" 'Body of mail message
.Send 'Send the message
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
Sub GetFolderContents ()
Dim objOutlook As New Outlook.Application
Dim objNameSpace As NameSpace
Dim objFolder As MAPIFolder
Dim i As Integer
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = _
objNameSpace.GetDefaultFolder(olFolderInbox) 'Access the Inbox
For i = 1 To objFolder.Items.Count 'Loop through all
Debug.Print objFolder.Items(i).Subject 'items in the Inbox
Next 'and print the
'Subject line
Set objFolder = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
End Sub
If you wish to access a folder that is not a default folder, traverse the
object model to get the folder object. For example, if you wish to access a
folder "Personal" under the message store "Mailbox - John Doe," you could
use the following:
Set objFolder = _
objNameSpace.Folders("Mailbox - John Doe").Folders("Personal")
NOTE: Folder names are case-sensitive.
Sub AddAppointment()
Dim objOutlook As New Outlook.Application
Dim objAppt As AppointmentItem
Set objAppt = _
objOutlook.CreateItem(olAppointmentItem) 'Create a new
'ContactItem
With objAppt
.Subject = "Lunch" 'Appointment's Subject line
.Start = #2/25/97 1:00:00 PM# 'Appointment's starting
'time
.End = #2/25/97 2:00:00 PM# 'Appointment's ending time
.Location = "Restaurant" 'Appointment's location
.ReminderSet = True 'Set Reminder to 15 minutes
.ReminderMinutesBeforeStart = 15 'before the start of the
'appointment
.Save 'Save the appointment
End With
Set objAppt = Nothing
Set objOutlook = Nothing
End Sub
[Start] >= "2/25/97 12:00 AM" and [Start] <= "2/25/97 11:59 PM"With the Find method, the first item matching the criteria is returned. To find subsequent matches, you must use the FindNext method. FindNext will return Nothing when no more matches are found:
Sub GetAppointments()
Dim objOutlook As New Outlook.Application
Dim objNS As NameSpace
Dim Appt As Object
Dim objInboxItems As Items
Dim Criteria as String
Set objNS = objOutlook.GetNamespace("MAPI")
Set objInboxItems = _
objNS.GetDefaultFolder(olFolderCalendar).Items 'Get all items
'in Calendar folder
Criteria = "[Start]>=""2/25/97 12:00 AM"" " & _
"and [Start]<= ""2/25/97 11:59 PM""" 'Criteria for Find
'method
Set Appt = objInboxItems.Find(Criteria) 'Find first Appointment
'matching the criteria
Do While Not(Appt Is Nothing) 'Loop until no match
Debug.Print Appt.Start; Appt.End; Appt.Subject 'Print the
'Appointment Start,
'End, and Subject
Set Appt = objInboxItems.FindNext 'Find the next
'Appointment matching
'the same criteria
'used with the Find
'method
Loop
Set Appt = Nothing
Set objInboxItems = Nothing
Set objNS = Nothing
Set objOutlook = Nothing
End Sub
Sub AddContact()
Dim objOutlook As New Outlook.Application
Dim objContact As ContactItem
Set objContact = objOutlook.CreateItem(olContactItem)
With objContact
.FirstName = "John" 'First Name is "John"
.LastName = "Doe" 'Last Name is "Doe"
.HomeTelephoneNumber = "123-456-7890" 'Home Telephone Number is
'"123-456-7890"
.HomeAddressStreet = "123 Main St." 'Street for the Home
'Address is "123 Main
'St."
.HomeAddressCity = "Anycity" 'City for the Home
'Address is "Anycity"
.HomeAddressState = "WA" 'State for the Home
'Address is "WA"
.HomeAddressPostalCode = "98765" 'Postal Code for the Home
'Address is "98765"
.Save 'Save the new contact
End With
Set objContact = Nothing
Set objOutlook = Nothing
End Sub
Sub GetContactInfo()
Dim objOutlook As New Outlook.Application
Dim objContact As ContactItem
Dim objNameSpace As NameSpace
Dim objFolder As MAPIFolder
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = _
objNameSpace.GetDefaultFolder(olFolderContacts)'The Contacts
'Folder
With objFolder.Items("John Doe") 'Print the
'HomeAddress
Debug.Print .HomeAddress 'and the
Debug.Print .HomeTelephoneNumber 'HomeTelephoneNumber
End With 'for the Contact with
'the Name "John Doe"
Set objFolder = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
End Sub
162671 OL97: Visual Basic Help File for Microsoft Outlook Available
Additional query words: OutSol OutSol97 OutSol98
Keywords: kbautomation kbhowto kbinterop kbprogramming KB170262