VBA Basics

There are a few basic concepts you need to know in order to program in VBA. Variables are used to store temporary data. Object variables are used to reference objects such as the Outlook Application object, the MailItem object, or a Reminder object.

A procedure is a named set of instructions that's executed as a unit. There are two main types of procedures you'll use in VBA: subs and functions. A sub is generally a procedure that does not return a value. You might change variable values or perform actions, but the actual set of commands does not produce an ending value. A typical Outlook VBA sub might look like the following:

 Sub SendMail() Dim olItem As Outlook.MailItem Set olItem = Application.CreateItem(olMailItem) olItem.To = "Patricia Cardoza" olItem.Subject = "Meeting Minutes" olItem.Send End Sub 

A function, on the other hand, is a set of instructions that returns an end value. You might call a function within a sub to return a value based on a condition. The following function returns True if Outlook is operating with a connection to an Exchange Server. If Outlook is running in offline mode, the function returns False.

 Function IsOutlookOnline() Dim objSession as MAPI.Session Dim objInfoStore as MAPI.InfoStore Dim bolOffline as Boolean Set objSession = CreateObject("MAPI.Session") objSession.Logon "", "", False, False Set objInfoStore = objSession.GetInfoStore(objSession.Inbox.StoreID)     'Check if it's offline     bolOffline = objInfoStore.Fields(&H6632000B) 'PR_STORE_OFFLINE     If bolOffline Then         IsOutlookOnline = False     Else         IsOutlookOnline = True     End If Set objInfoStore = Nothing Set objSession = Nothing End Function 

There's a lot more to VBA than just subs and functions. To use the examples in this chapter, you'll have to know a little more than just those aspects. The examples in this chapter could be characterized as intermediate examples. If you're not familiar with VBA code and want to automate Outlook with VBA, I suggest investing in a good VBA book.



Special Edition Using Microsoft Office Outlook 2003
Special Edition Using Microsoft Office Outlook 2003
ISBN: 0789729563
EAN: 2147483647
Year: 2003
Pages: 426

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net