PDA

View Full Version : Help with script to copy mail



rrands1
06-24-2017, 03:40 PM
Hi all - with some samples from this forum, I have the following code. I want to copy mail as it enters a folder (for my test I am using the inbox, but eventually want this to be for the Sent and Deleted items) over to another folder. I am getting an error "91 - Object variable or With block variable not set". I have a folder named "test" in the folder hierarchy at the same level as Inbox (in the code I replaced my actual email address with 'emailaddresshere', but what I was using was the name at the top of my folder list in Outlook). This is for Outlook 2016. Any ideas what I have wrong?

Thank you very much for any help!

-randy

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")


' (1) default Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler


' (2) only act if it's a MailItem
Dim Msg As Outlook.MailItem
Dim fldr As Outlook.MAPIFolder

If TypeName(item) = "MailItem" Then


Set fldr = Outlook.Session.Folders("emailaddresshere").Folders("Test") ' problem is on this line I think...
Msg.Copy fldr
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub

gmayor
06-24-2017, 10:15 PM
The idea is OK, but the methodology awry. Try the following


Option Explicit
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
' (1) default Inbox
Set objNS = Outlook.GetNamespace("MAPI")
Set objItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objItems_ItemAdd(ByVal olItem As Object)
Dim objNS As Outlook.NameSpace
Dim fldr As Outlook.MAPIFolder
Dim oStore As Store
Dim olCopy As MailItem
On Error GoTo ErrorHandler
Set oStore = Session.GetDefaultFolder(olFolderInbox).Store
Set fldr = oStore.GetRootFolder.folders("Test")
' (2) only act if it's a MailItem
If TypeName(olItem) = "MailItem" Then
'create a copy of the message
Set olCopy = olItem.Copy
'move the copy to the folder
olCopy.Move fldr
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub

rrands1
06-25-2017, 09:55 PM
[Hi, Graham - thank you very much for the reply! I have two more questions for you if I may? First, I put this in, and it does put a copy of the mail from the inbox, however it is leaving the original item in the inbox - any idea how to have it move it (or delete the original and bypass the recycle bin)?

Secondly, my main goal here is this:
1. When an item gets sent, move the item from the original Sent Items to another folder in a 2nd account in outlook. (my other email account)
2. When I delete an item (so it shows up in the deleted items now), move that from there to another folder in a 2nd account in outlook.

This should work no matter whether I send from this machine, or even if i sent or deleted from my iPhone - when it syncs to this Outlook, I want it to process each new item coming into the Sent and Deleted items & move it over. Hopefully that makes sense. I have been able to change your script so that it monitors the Sent Items (not sure how to monitor both yet), and anything I send from this PC will move to the Test folder, however I have not been successful getting it to identify (via code) the 2nd mailbox to store it there. Also, when I send from my iphone, while the item shows up in sent items here, it does not process this script to move the mail.

Any thoughts on that? I really appreciate the help! I have tried, but this seems to be beyond my meager skills in this area... :S


Thank you!!!


-randy

gmayor
06-25-2017, 10:38 PM
If you want to delete the message after moving the copy, then just add the line

olItem.Deleteimmediately after the line
olCopy.Move fldr
If you want to delete the item without it appearing in the deleted items folder then you will also need to delete it from that folder e.g.

Session.GetDefaultFolder(olFolderDeletedItems).Items(1).Delete
To get a folder in another store you must address the store as in the previpus copde e.g.

Set oFolder = Session.Stores("StoreName").GetRootFolder.folders("FolderName")will give you the folder FolderName at the root level of the store StoreName

The macro runs from Outlook on the PC that runs the code. It has no bearing on what happens to messages processed by your iPhone.