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!
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!