从Excel创建和分配Outlook中的任务

时间:2016-02-28 23:44:14

标签: excel vba excel-vba outlook

我正在尝试在Excel中创建一个服务台工具,而我想让管理员轻松分配它们的方式就是使用Outlook中的任务。

管理员将收到电子邮件,在Excel中填写一行,然后单击按钮在Outlook中打开新任务,并预先填写字段。然后他们会将电子邮件作为附件拖放。

但是,我正在尝试从电子表格中的值预先填充指定的电子邮件地址。分配似乎不起作用,收件人也不起作用。有没有人有任何想法为什么不呢?这是我到目前为止的代码:

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

 Set OutApp = CreateObject("Outlook.Application")
 Set OutTask = OutApp.CreateItem(olTaskItem)

 With OutTask
    .Assign = Cells(2, "G")
    .Subject = "Service Request" & Cells(2, "A")
    .StartDate = Cells(2, "H")
    .DueDate = Cells(2, "H") + Cells(2, "I")
    .ReminderTime = .DueDate - 1
    .Body = "Please see the attached email for a service request assigned to you."
    .Display
 End With

Set OutTsk = Nothing
Set OutApp = Nothing

End Sub

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在这里查看示例https://msdn.microsoft.com/en-us/library/office/ff869880(v=office.15).aspx 由微软提供 分配是一种方法,您正在使用它作为属性。 收据是一个集合,所以你必须添加它们,再次你不能像属性一样分配它们 你最后也有拼错OutTsk。打开编程时我会做的事情是Option Explicit,这会阻止拼写错误的变量毁了你的一天。

所以尝试这样的事情

Sub tasks()
Dim OutApp As Outlook.Application
Dim OutTask As Outlook.TaskItem

Set OutApp = CreateObject("Outlook.Application")
Set OutTask = OutApp.CreateItem(olTaskItem)
Dim myRecipient As Outlook.Recipient
Set myRecipient = OutTask.Recipients.Add(Cells(2, "C"))
myRecipient.Resolve

 If myRecipient.Resolved Then
    With OutTask
       .Subject = "Service Request" & Cells(2, "A")
       .StartDate = Cells(2, "H")
       .DueDate = Cells(2, "H") + Cells(2, "I")
       .ReminderTime = .DueDate - 1
       .Body = "Please see the attached email for a service request assigned to you."
       .Assign
       .Display
    End With
End If
Set OutTask = Nothing
Set OutApp = Nothing

End Sub