VB、VBS和VBA中使用outlook学习(一)

一、初识「Outlook.Application对象的使用

⇒演示如何在vb中创建一个新outlook任务

'---------------------------------------------------------------------
'演示如何创建一个新任务
'---------------------------------------------------------------------

Dim xOutLook As New Outlook.Application
Dim xMail As MailItem

Dim SUBJECT_MATTER As String
Dim PLACE As String
Dim START_TIME As String
Dim DURATION_MINUTES As Long
Dim MINUTES_BEFORE_START As Long
Dim BODY_TEXT


Private Sub Command1_Click()
    '创建一个新任务
    Set objTaskItem = xOutLook.CreateItem(ItemType:=olTaskItem)

    With objTaskItem
        .Subject = SUBJECT_MATTER
        '.Location = PLACE
        .StartDate = START_TIME
        '.Duration = DURATION_MINUTES
        '.ReminderMinutesBeforeStart = MINUTES_BEFORE_START
        '.BusyStatus = olOutOfOffice
        .Body = BODY_TEXT
        .Sensitivity = olPrivate
        .Save
        .Display
    End With
End Sub

Private Sub Form_Load()
    SUBJECT_MATTER = "午餐会"
    PLACE = "Championzone 餐厅"
    START_TIME = #8/13/2001 6:00:00 PM#
    DURATION_MINUTES = 90
    MINUTES_BEFORE_START = 45
    BODY_TEXT = "别忘记带礼物!"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    '退出Outlook
    'xOutLook.Quit
    Set xMail = Nothing
    Set xOutLook = Nothing
End Sub

Private Sub List1_Click()
    'Text1.Text = List1.List(List1.ListIndex)
End Sub
 

操作olMailItem的例子:创建邮件信息文件 

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Item.Categories = ""
    If Item.Class = olMail Then
        Call CheckToSave(Item)
    End If
End Sub

Public Sub CheckToSave(objMail As MailItem)
    Dim Msg, Style, Title, Response, MyString, strSaveName
    Msg = "Do you want to save a copy of this email to file?"    ' Define message.
    Style = vbYesNo + vbQuestion + vbDefaultButton1    ' Define buttons.
    Title = "Mailbox Control"    ' Define title.
    strSaveName = "C:/Temp/MySavedMessage.txt" 'Change this to change the path and name of the file.
       
        ' Display message.
    Response = MsgBox(Msg, Style, Title)

    If Response = vbYes Then    ' User chose Yes.
        objMail.SaveAs strSaveName, olTXT
    Else    ' User chose No.
   
    End If
End Sub

Private Sub Command2_Click()
    Set xMail = xOutLook.CreateItem(ItemType:=olMailItem)
    With xMail
        xMail.To = Text1.Text
        xMail.Subject = Text2.Text
        xMail.Body = Text3.Text
    End With

    Call Application_ItemSend(xMail, True)
End Sub 

你可能感兴趣的:(VB)