Ask about a simple script about save a incoming mail as file on local PC

Kam

New Member
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
Hello everyone,

Nice to see you! I'm just start learning about vba in outlooks. Now I want to write a very simple script which can do: when the mail (email1) arrives, use outlook rules to move it to another outlook folder(folder2, it is a sub folder of Inbox) run a this script to save this mail as a txt file on PC.
I write script like this:

Public Sub saveThisMail(Item As Outlook.MailItem)
Dim myolApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim targetFolder As Outlook.Folder
Dim targetMail As Outlook.MailItem

Set myNameSpace = myolApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set targetFolder = myFolder.Folders("FOLDER2")
Set targetMail = targetFolder.Items.GetLast
targetMail.SaveAs "D:\" & "savedmail" & ".TXT", olTXT
msgbox ("mail saved")
End Sub


I set rule to when mail arrives, move it to folder2 and run a script: saveThisMail.
But it always save the second last mail, not the just arriving one.
From my eyes' looking, in folder2, the new mail always appears after the msgbox dsappears.
So i was wondering the script is executed before the new mail arrive and move to new folder.
So could someone who can give me some advice how could I achieve my target?
Thanks in advance!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It looks like Set targetMail = targetFolder.Items.GetLast is getting the "current" last message. When you use a run a rule script, you should use the rule to set the conditions and do all of the actions in the script.

Set targetFolder = myFolder.Folders("FOLDER2")
item.SaveAs "D:\" & "savedmail" & ".TXT", olTXT
item.move targetfolder


This should work -

Code:
Public Sub saveThisMail(Item As Outlook.MailItem) 
Dim myFolder As Outlook.Folder 
Dim targetFolder As Outlook.Folder 
Dim targetMail As Outlook.MailItem 
 
Set myFolder = Session.GetDefaultFolder(olFolderInbox) 
Set targetFolder = myFolder.Folders("FOLDER2") 
item.SaveAs "D:\" & "savedmail" & ".TXT", olTXT 
msgbox ("mail saved") 
item.move targetfolder 
 
End Sub
As an FYI, if you wanted to use your method even though it's not the most direct, you'd need to get the EntryID before the message was moved and find it based on the entryid, not last message. This code has an entryid sample:
http://www.slipstick.com/developer/move-messages-cc-address/
 

Kam

New Member
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
Thank you very much, it is working!
And I think I know the different between item and targetMail now(which I didn't) :p
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
And I think I know the different between item and targetMail now(which I didn't) :p
there really isn't much of a difference - except that in this case, the code is identifying item as the item that just met the condition of the rule. to use targetmail, you need to set it as the item: set targetmail = item (which is silly since it just adds another line to the code), or if you are running a macro manually, you'd tell outlook it was current item using one of several methods.
 
Top