-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutlookTaskFromEmailNestedTaskFolder.bas
61 lines (48 loc) · 2.02 KB
/
OutlookTaskFromEmailNestedTaskFolder.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Sub CreateTaskFromEmail()
Dim objMail As MailItem
Dim objTask As TaskItem
Dim objNamespace As NameSpace
Dim objTaskFolder As Folder
Dim strCategory As String
' Specify the target Task Folder name
Const TARGET_FOLDER_NAME As String = "INSERT TASK FOLDER NAME"
' Specify the category to apply
strCategory = "INSERT CATEGORY"
' Handle errors while working with objects
On Error Resume Next
' Get the currently selected item in Outlook
Set objMail = Application.ActiveExplorer.Selection.Item(1)
' Check if an email is selected
If objMail Is Nothing Then
MsgBox "Please select an email.", vbInformation
Exit Sub
End If
' Check if the selected item is a mail item
If objMail.Class = olMail Then
' Get the MAPI namespace
Set objNamespace = Application.GetNamespace("MAPI")
' Locate the target task folder
Set objTaskFolder = objNamespace.GetDefaultFolder(olFolderTasks).Folders(TARGET_FOLDER_NAME)
' Check if the target task folder exists
If objTaskFolder Is Nothing Then
MsgBox "The specified task folder does not exist. Please check the folder name.", vbInformation
Exit Sub
End If
' Create a new task item in the target task folder
Set objTask = objTaskFolder.Items.Add(olTaskItem)
With objTask
.Subject = objMail.Subject ' Set the task subject to the email subject
.StartDate = objMail.ReceivedTime ' Set the start date of the task
.Categories = strCategory ' Set the task category
.Display ' Open the task window for editing
End With
MsgBox "A new task has been created in the specified folder.", vbInformation
Else
MsgBox "Please select an email.", vbInformation
End If
' Release the object variables
Set objMail = Nothing
Set objTask = Nothing
Set objNamespace = Nothing
Set objTaskFolder = Nothing
End Sub