sanders
sanders

Reputation: 10888

Getting the incoming email in outlook via VBA

I am trying to save incoming messages via outlook to my local file system. The code I have so far is:

Sub save_to_dir(Item As Outlook.MailItem)
 'the mail we want to process
Dim objItem As Outlook.MailItem
 'question for saving, use subject to save
Dim strPrompt As String, strname As String
 'variables for the replacement of illegal characters
Dim sreplace As String, mychar As Variant, strdate As String
 'put active mail in this object holder 

Set objItem = Outlook.ActiveExplorer.Selection.Item(1)


 'check if it's an email ... need to take a closer look cause
 'gives an error when something else (contact, task) is selected
 'because objItem is defined as a mailitem and code errors out
 'saving does work, if you take care that a mailitem is selected
 'before executing this code
 mypath = "c:\temp\outlook\"
If objItem.Class = olMail Then
    ' check on subject
    If objItem.Subject <> vbNullString Then
        strname = objItem.Subject
    Else
        strname = "No_Subject"
    End If
    strdate = objItem.ReceivedTime
     'define the character that will replace illegal characters
    sreplace = "_"
     'create an array to loop through illegal characters (saves lines)
    For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
         'do the replacement for each character that's illegal
        strname = Replace(strname, mychar, sreplace)
        strdate = Replace(strdate, mychar, sreplace)
    Next mychar
     'Prompt the user for confirmation
    'strPrompt = "Are you sure you want to save the item?"
    'If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
        objItem.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG
    ' Else
    '    MsgBox "You chose not to save."
    'End If
End If
End Sub

Problem with this code is that when you have an item selected in your outlook on the moment the email comes in it uses the selected item in stead of the incoming mail

How can i get the incoming email?

Thanks

EDIT

After debugging this line Set objItem = Outlook.ActiveExplorer.Selection.Item(1)

I found out that Outlook.ActiveExplorer.Selection.Item(1) has got the current email in it, but when i look into objItem after it executed the line the value of objItem is the email which is currently selected in outlook and not the incoming email.

Any idea's?

Upvotes: 2

Views: 4765

Answers (2)

sanders
sanders

Reputation: 10888

a good article to reference can be found here : http://www.outlookcode.com/article.aspx?id=62

I have adapted sample one as follows:

Sub save_to_dir_test1(mymail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem

strID = mymail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
mypath = "c:\temp\outlook\"
strdate = objMail.ReceivedTime
If objMail.Subject <> vbNullString Then
        strname = objMail.Subject
   Else
        strname = "No_Subject"
End If
sreplace = "_"
'create an array to loop through illegal characters (saves lines)
For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
'do the replacement for each character that's illegal
   strname = Replace(strname, mychar, sreplace)
   strdate = Replace(strdate, mychar, sreplace)
Next mychar
objMail.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG

Set objMail = Nothing
End Sub

Upvotes: 1

Larry
Larry

Reputation: 2794

you can set a "Rule" in outlook on incoming email,select "run a script" in action , choice the sub below

And put the sub below in a module of thisoutlooksession module

Sub testing(MyMail As MailItem)
 MyMail.SaveAs ' your path here
end sub

Hope it helps

Upvotes: 0

Related Questions