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

Status
Not open for further replies.

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.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S Ask user to input email template through VBA Outlook VBA and Custom Forms 1
C Reports always ask 'Would you like to save your changes?' BCM (Business Contact Manager) 1
D VBA Script (Ask to where to save send mail) Outlook VBA and Custom Forms 1
S Trying to have a prompt to ask for text to be added to subject before sending. Using Outlook 3
S I am embarassed to ask.. Exchange Server Administration 2
C Ask to Ribbons Outlook VBA and Custom Forms 2
GregS Simple calendar sharing doesn't work Using Outlook 6
F Simple code to look for a specific line Outlook VBA and Custom Forms 2
P Simple contact and project manger like MissingLink for Outlook Using Outlook 1
L Simple Macro Using Outlook 1
B Is there a simple way to share email folders w/o exchange server? Using Outlook 0
C Need Help with Simple Code Correction Using Outlook 11
F I want simple syncing between my 3 PCs and Android devices. Using Outlook 1
H Too simple I would think but.. Using Outlook 1
D Help with simple macro - looping though all emails in my inbox Using Outlook 3
T Location and simple copying of emails and settings, OL 2010 and Win 7 64bit. Using Outlook 3
R Simple Sharing of Outlook Profile Using Outlook 1
Rupert Dragwater Is there a genuine simple way to move Eudora address to Outlook 2010 Using Outlook 2
R BCM with MS Office Accounting... Simple question BCM (Business Contact Manager) 0
C Trying to make a simple custom outlook form Using Outlook 2
P Simple Marketing Campaign Question BCM (Business Contact Manager) 1
B Simple way to insert file text - macro? Outlook VBA and Custom Forms 1
V simple move to personal folder Outlook VBA and Custom Forms 1
T Simple MAPI send and Outlook COM addin Outlook VBA and Custom Forms 3
D.Moore VB script to Digitaly Sign newly created outlook message Outlook VBA and Custom Forms 2
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
D.Moore VBA script fail after Office 365 update Using Outlook 8
M Outlook 2013 Script Assistance - Save Opened Link with Subject Added Outlook VBA and Custom Forms 1
F Script for zip file attachment Outlook VBA and Custom Forms 1
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
Y Outlook 2013 Run A Script Outlook VBA and Custom Forms 4
Z Script to set account? Using Outlook 0
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
J Calling a Public sub-routine from the script editor via VB script Outlook VBA and Custom Forms 4
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
Peter H Williams Enable script containing VBA Outlook VBA and Custom Forms 12
H VB script in outlook form doesn't work anymore Outlook VBA and Custom Forms 2
A Script to fetch data from mails in restricted collection and sending them to excel Using Outlook 1
B Wanting to run a script that will filter any body that has a russian link in it. Outlook VBA and Custom Forms 5
Bri the Tech Guy Registry Tweak to make "Run a Script" Action Available Outlook VBA and Custom Forms 2
V VB script code to save a specific email attachment from a given email Outlook VBA and Custom Forms 14
Bri the Tech Guy Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
M Subject Line Automation - Trigger Script Delayed Outlook VBA and Custom Forms 2
Q Script to create a pst file for Archiving Using Outlook 1
Vijay Error in rule- Run a script Using Outlook 1
R VBA Script Quick Parts Using Outlook 1
Vijay Run script doesn't work in outlook Using Outlook 1
Q VBA Script to move item in secondary mailbox Outlook VBA and Custom Forms 2

Similar threads

Top