Make Outlook Task item through Excel Button

2019-08-27 23:36发布

问题:

I want to make a simple program in which I can create an Outlook Task. User fills in data and clicks the create button.

I found the following code online and it should work, but it doesn't. No task is added in my Outlook, but no error is shown either. I have the feeling it somehow goes wrong with adding the recipients of the task.

Any clue why I don't get an error but, no tasks are added?

Dim OutApp As Outlook.Application
Dim OutTask As Outlook.TaskItem


Set OutApp = CreateObject("Outlook.Application")
Set OutTask = OutApp.CreateItem(olTaskItem)
Set myRecipient = OutTask.Recipients.Add("I.wont.write.my.actual.address.in.this@example.com")
myRecipient.Type = olTo



 If myRecipient.Resolved Then
    With OutTask
    .Display
       .Subject = Cells(3, "I")
       .StartDate = Now
       .DueDate = Cells(2, "I")
       .Body = "Please see the attached email for a service request assigned to you."
    End With
End If
Set OutTask = Nothing
Set OutApp = Nothing

I just can't figure it out and it's really breaking my brain at the moment. Hope someone can hint me in the right direction!

回答1:

I found the following code online and it should work, but it doesn't. No task is added in my Outlook, but no error is shown either. I have the feeling it somehow goes wrong with adding the recipients of the task.

Correct - Attempt to resolve the Recipient object myRecipient.Resolve against the Address Book before assuming its resolved If myRecipient.Resolved Then also defined variable Dim myRecipient As Outlook.Recipient for myRecipient

Option Explicit
Sub tasks()
    Dim OutApp As Outlook.Application
    Set OutApp = CreateObject("Outlook.Application")

    Dim OutTask As Outlook.TaskItem
    Set OutTask = OutApp.CreateItem(olTaskItem)

    Dim myRecipient As Outlook.Recipient
    Set myRecipient = OutTask.Recipients.Add("0m3r@Email.com")
        myRecipient.Type = olTo
        myRecipient.Resolve

     If myRecipient.Resolved Then
        With OutTask
        .Display
           .Subject = Cells(3, "I")
           .StartDate = Now
           .DueDate = Cells(2, "I")
           .Body = "Please see the attached email."
        End With
    End If

    Set OutTask = Nothing
    Set OutApp = Nothing
End Sub

Option Explicit Statement (Visual Basic)

Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.



回答2:

Check this examples, run each one and see if they are usefull to you, hope one fit your needs:

Sub outlook_send_followup()

' High importance = 2
' Nothing = 1
' Low importance = 0

Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")

Dim MyItem As Object
Set MyItem = OutApp.CreateItem(olMailItem)

    With MyItem
            .To = "example@hotmail.com"
            .Subject = "hi, this is a task"
            .SentOnBehalfOfName = "example@hotmail.com"
            .HTMLBody = "<HTML MSG FORMAT HERE>"
            .Importance = 1
            .FlagStatus = olFlagMarked
            .FlagRequest = "Follow up"
            .FlagDueBy = Now
            .Display
    End With

Set MyItem = Nothing
Set OutApp = Nothing

End Sub

Sub create_outlook_taks()

'Const olImportanceLow = 0
'Const olImportanceNormal = 1
'Const olImportanceHigh = 2

Dim outlook_app As Object
Set outlook_app = CreateObject("Outlook.Application")

    With outlook_app.CreateItem(3)
        .Importance = 2
        .Subject = "THIS IS A TASK"
        .StartDate = Now + 5
        .DueDate = Now + 10
        .ReminderTime = Now - 3
        .Body = "HI YOU CREATED THIS TASK"
        .Display
        '.Save
    End With

Set outlook_app = Nothing

End Sub