本文示範如何使用 Visual Basic 的「自動化」功能,以程式來控制 Microsoft
Outlook。本範例示範利用 Microsoft Outlook 的物件模式來建立聯絡人、建立約會與傳送訊息。
依照下列步驟來建立與執行此範例。如果要執行此範例,您需要 Microsoft Outlook
型別程式庫的早期繫結參考。下列表格列出不同版本的 Microsoft Outlook 型別程式庫檔名:
摺疊此表格展開此表格
| Outlook 版本 | 型別程式庫在參考清單中的顯示方式 | 檔名 |
|---|
| Outlook 97 | "Microsoft Outlook 8.0
物件程式庫" | msoutl8.olb |
| msoutl8.olb | "Microsoft Outlook 98
物件程式庫" | msoutl85.olb |
| Outlook 2000 | "Microsoft Outlook 9.0
物件程式庫" | msoutl9.olb |
| Outlook 2002 | "Microsoft Outlook 10.0
物件程式庫" | msoutl.olb |
| Office Outlook 2003 | "Microsoft Outlook 11.0
物件程式庫" | msoutl.olb |
建立自動化範例
- 開啟 Visual Basic 然後建立新的「標準執行檔」專案。
- 從 [專案] 功能表中,選擇 [參考] 再選取 [Microsoft Outlook]。
- 將按鈕新增到表格中。
- 按兩下按鈕,然後新增下列程式碼:
' 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 = "James Smith"
.Birthday = "9/15/1975"
.CompanyName = "Microsoft"
.HomeTelephoneNumber = "704-555-8888"
.Email1Address = "someone@microsoft.com"
.JobTitle = "Developer"
.HomeAddress = "111 Main St." & vbCr & "Charlotte, NC 28226"
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 = "Meeting to discuss plans..."
.Body = "Meeting 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 meeting..."
olMail.Body = _
"Dear " & olItem.FirstName & ", " & vbCr & vbCr & vbTab & _
"I'll see you in 2 minutes for our meeting!" & vbCr & vbCr & _
"Btw: I've added you to my contact list."
olMail.Send
' Clean up...
MsgBox "All done...", vbMsgBoxSetForeground
olNS.Logoff
Set olNs = Nothing
Set olMail = Nothing
Set olAppt = Nothing
Set olItem = Nothing
Set olApp = Nothing
- 執行該專案,並按一下按鈕來執行程式碼。
當程式碼執行後,應會出現命名為「James Smith」的新連絡人、排定一個會每分鐘發出提醒、兩分鐘後的約會,並寄出訊息到
someone@microsoft.com。此外,因為新增連絡人的生日 (9/15),因此週期性事件已新增到您的 Outlook
行事曆用以提醒。
Outlook 2002 有兩個新的對話方塊:一個是警告您有程式正嘗試存取 Outlook
中的電子郵件地址,並詢問您是否允許存取,另一個訊息大意是有程式正嘗試發送電子郵件。這個功能將保護您免於在不知情的情況下遭病毒從系統發送電子郵件。
如需詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
290500
(http://support.microsoft.com/kb/290500/
)
說明 Outlook 2002 中供開發人員使用的電子郵件安全性功能
如需有關 Outlook 程式設計資源的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
166368
(http://support.microsoft.com/kb/166368/
)
Resources for custom forms and programming in Outlook 97
180826
(http://support.microsoft.com/kb/180826/
)
Resources for custom forms and programming in Outlook 98
271225
(http://support.microsoft.com/kb/271225/
)
Resources for custom forms and programming in Outlook 2000
287531
(http://support.microsoft.com/kb/287531/
)
List of resources for custom forms and programming with Outlook 2002
313802
(http://support.microsoft.com/kb/313802/
)
How to retrieve contacts by using Outlook Object Model in Visual Basic .NET
313788
(http://support.microsoft.com/kb/313788/
)
How to create an appointment by using Outlook Object Model in Microsoft Visual Basic .NET
文章編號: 220595 - 上次校閱: 2006年3月23日 - 版次: 9.0
這篇文章中的資訊適用於:
- Microsoft Visual Basic 6.0 Enterprise Edition
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Office Outlook 2003
- Microsoft Outlook 2002 Standard Edition
- Microsoft Outlook 2000 Standard Edition
- Microsoft Outlook 97 Standard Edition
- Microsoft Outlook 98 Standard Edition
| kbautomation kbhowto kboutlookobj KB220595 |
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。