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
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!
 
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/
 
Thank you very much, it is working!
And I think I know the different between item and targetMail now(which I didn't) :p
 
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
U Disable "Always ask before opening" Dialog Using Outlook 3
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
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
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
R Script for simplifying spam control Outlook VBA and Custom Forms 8
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
N Outlook 2021 'Run Script" Rules? Outlook VBA and Custom Forms 4
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
W Designer Form 2013 and Script ? how ? Outlook VBA and Custom Forms 1
G print attachment straight away; working script edit not working Outlook VBA and Custom Forms 0
G Save attachment run a script rule Outlook VBA and Custom Forms 0
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
G Script does not exist Outlook VBA and Custom Forms 0
G Trigger script without restaring outlook Outlook VBA and Custom Forms 7
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
L VB script only runs manually Outlook VBA and Custom Forms 5
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
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

Similar threads

Back
Top