如何在Excel中调用Outlook,添加任务或日程
已经有好几个人询问如何在Excel中使用VBA调用Outlook或者其它Office应用程序,其实做法很简单,关键还是对于目标应用程序的对象模型的掌握和使用,这里有一个以前做过的例子,是在Excel中做一个计划表,然后调用Outlook,将计划添加到任务,因为代码是自己当时用的,所以比较粗糙。
Excel表格:
代码:
Public Sub WriteToOutlookTask()
Dim Task As Outlook.TaskItem
Dim rng As Range
Dim i As Long
For i = 3 To 34 Step 1
If Range("B" & i).Value <> "" Then
Set Task = Outlook.Application.CreateItem(olTaskItem)
Task.Subject = "初记:" & Range("B" & i).Value & "-" & Range("C" & i).Value
Task.StartDate = Range("A" & i).Value
Task.DueDate = Range("A" & i).Value
Task.Save
End If
If Range("D" & i).Value <> "" Then
Set Task = Outlook.Application.CreateItem(olTaskItem)
Task.Subject = "复习:" & Range("B" & i - 2).Value & "-" & Range("C" & i - 2).Value
Task.StartDate = Range("A" & i).Value
Task.DueDate = Range("A" & i).Value
Task.Save
End If
If Range("E" & i).Value <> "" Then
Set Task = Outlook.Application.CreateItem(olTaskItem)
Task.Subject = "复习:" & Range("B" & i - 4).Value & "-" & Range("C" & i - 4).Value
Task.StartDate = Range("A" & i).Value
Task.DueDate = Range("A" & i).Value
Task.Save
End If
Next
End Sub
Dim Task As Outlook.TaskItem
Dim rng As Range
Dim i As Long
For i = 3 To 34 Step 1
If Range("B" & i).Value <> "" Then
Set Task = Outlook.Application.CreateItem(olTaskItem)
Task.Subject = "初记:" & Range("B" & i).Value & "-" & Range("C" & i).Value
Task.StartDate = Range("A" & i).Value
Task.DueDate = Range("A" & i).Value
Task.Save
End If
If Range("D" & i).Value <> "" Then
Set Task = Outlook.Application.CreateItem(olTaskItem)
Task.Subject = "复习:" & Range("B" & i - 2).Value & "-" & Range("C" & i - 2).Value
Task.StartDate = Range("A" & i).Value
Task.DueDate = Range("A" & i).Value
Task.Save
End If
If Range("E" & i).Value <> "" Then
Set Task = Outlook.Application.CreateItem(olTaskItem)
Task.Subject = "复习:" & Range("B" & i - 4).Value & "-" & Range("C" & i - 4).Value
Task.StartDate = Range("A" & i).Value
Task.DueDate = Range("A" & i).Value
Task.Save
End If
Next
End Sub
完整的Excel文件下载:ExcelOutlook