azhumvee
07-05-2014, 09:16 PM
So I have 2 separate macros that generally do what I want them to when ran separately.. except for the EntryID issue that i'll explain next.. heres my scenario..
I want to be able to pick a message from my Inbox that is an "Action" item.. I want to be able to click a Macro button and have it:
Move to a pre-specified folder (see MoveToFiled code below)
then after it moves, (so as to get the "latest" entryID), creates a task like the 2nd part of the macro below (MoveSelectedMailtoTask)
Right now I don't even have the knowledge to combine these two macros as one action, and even if I did.. after Outlook moves the email to the prespecified folder, it loses "focus" on the mail so then it wouldn't create a task for the email that I want it to.. Hope this makes some sense.. Thx in advance for any input!
Sub MoveToFiled()
On Error Resume Next
Dim ns As Outlook.NameSpace
Dim moveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
Set ns = Application.GetNamespace("MAPI")
'Define path to the target folder
Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Folders("Actions")
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
If moveToFolder Is Nothing Then
MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
End If
For Each objItem In Application.ActiveExplorer.Selection
If moveToFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move moveToFolder
End If
End If
Next
Set objItem = Nothing
Set moveToFolder = Nothing
Set ns = Nothing
End Sub
Sub MoveSelectedMailtoTask()
Dim objTask As Outlook.TaskItem
Dim objMail As Outlook.MailItem
Set objTask = Application.CreateItem(olTaskItem)
For Each objMail In Application.ActiveExplorer.Selection
With objTask
.Subject = objMail.Subject
.StartDate = objMail.ReceivedTime
.Body = vbCrLf & vbCrLf & "url:outlook:" & objMail.EntryID & vbCrLf + objMail.Body
objMail.SaveAs attPath & objMail.EntryID
objTask.Attachments.Add attPath & objMail.EntryID, olEmbeddeditem
Kill (attPath & objMail.EntryID)
.Display
End With
'objMail.Delete
Next
Set objTask = Nothing
Set objMail = Nothing
End Sub
I want to be able to pick a message from my Inbox that is an "Action" item.. I want to be able to click a Macro button and have it:
Move to a pre-specified folder (see MoveToFiled code below)
then after it moves, (so as to get the "latest" entryID), creates a task like the 2nd part of the macro below (MoveSelectedMailtoTask)
Right now I don't even have the knowledge to combine these two macros as one action, and even if I did.. after Outlook moves the email to the prespecified folder, it loses "focus" on the mail so then it wouldn't create a task for the email that I want it to.. Hope this makes some sense.. Thx in advance for any input!
Sub MoveToFiled()
On Error Resume Next
Dim ns As Outlook.NameSpace
Dim moveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
Set ns = Application.GetNamespace("MAPI")
'Define path to the target folder
Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Folders("Actions")
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
If moveToFolder Is Nothing Then
MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
End If
For Each objItem In Application.ActiveExplorer.Selection
If moveToFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move moveToFolder
End If
End If
Next
Set objItem = Nothing
Set moveToFolder = Nothing
Set ns = Nothing
End Sub
Sub MoveSelectedMailtoTask()
Dim objTask As Outlook.TaskItem
Dim objMail As Outlook.MailItem
Set objTask = Application.CreateItem(olTaskItem)
For Each objMail In Application.ActiveExplorer.Selection
With objTask
.Subject = objMail.Subject
.StartDate = objMail.ReceivedTime
.Body = vbCrLf & vbCrLf & "url:outlook:" & objMail.EntryID & vbCrLf + objMail.Body
objMail.SaveAs attPath & objMail.EntryID
objTask.Attachments.Add attPath & objMail.EntryID, olEmbeddeditem
Kill (attPath & objMail.EntryID)
.Display
End With
'objMail.Delete
Next
Set objTask = Nothing
Set objMail = Nothing
End Sub