您目前已離線,請等候您的網際網路重新連線

如何使用 Visual Basic 使 Outlook 自動化

Office 2003 支援已結束

Microsoft 於 2014 年 4 月 8 日結束對 Office 2003 的支援。此變更已影響您的軟體更新和安全性選項。 瞭解這對您的意義為何且如何持續受保護。

結論
本文示範如何使用 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

建立自動化範例

  1. 開啟 Visual Basic 然後建立新的「標準執行檔」專案。
  2. [專案] 功能表中,選擇 [參考] 再選取 [Microsoft Outlook]。
  3. 將按鈕新增到表格中。
  4. 按兩下按鈕,然後新增下列程式碼:
     ' 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						
  5. 執行該專案,並按一下按鈕來執行程式碼。
當程式碼執行後,應會出現命名為「James Smith」的新連絡人、排定一個會每分鐘發出提醒、兩分鐘後的約會,並寄出訊息到 someone@microsoft.com。此外,因為新增連絡人的生日 (9/15),因此週期性事件已新增到您的 Outlook 行事曆用以提醒。

Outlook 2002 有兩個新的對話方塊:一個是警告您有程式正嘗試存取 Outlook 中的電子郵件地址,並詢問您是否允許存取,另一個訊息大意是有程式正嘗試發送電子郵件。這個功能將保護您免於在不知情的情況下遭病毒從系統發送電子郵件。

如需詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
290500說明 Outlook 2002 中供開發人員使用的電子郵件安全性功能
参考
如需有關 Outlook 程式設計資源的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
166368Resources for custom forms and programming in Outlook 97
180826 Resources for custom forms and programming in Outlook 98
271225 Resources for custom forms and programming in Outlook 2000
287531 List of resources for custom forms and programming with Outlook 2002
313802 How to retrieve contacts by using Outlook Object Model in Visual Basic .NET
313788 How to create an appointment by using Outlook Object Model in Microsoft Visual Basic .NET
內容

文章識別碼:220595 - 最後檢閱時間:03/23/2006 09:14:20 - 修訂: 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
意見反應