Tracking Mail items being moved to folders

Status
Not open for further replies.

novemberskye

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
Hi All,

I am looking to track some data when mail items are moved in Shared mail account to any subfolders within the account.
the code below is fired via Items_ItemAdd(ByVal item As Object)
- I don't want this restriction. I would like to track every time an item is moved anywhere within the shared mail account.

Issues:
1. Working on one by one movement from Inbox to 'Actioned' folder but not for multiple selected items (it's adding the number of selection correctly but the data it's recording is the same as the first item)
2. Restricted to moving from Inbox to Actioned

Help needed:
1. Code to allow to get "from" and "to" folders within the mail account
1.1 Code to get "from" folder name and "to" folder name
2. Code to record mail item data allowing multiple selected mail items


Appreciate your help in advance!!!


Code:
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim itm As Object
Dim msg As Outlook.MailItem
Dim sTime As String
Dim sSubj As String
Dim sCat As String
Dim sConID As String
 
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders("contoso@microsoft.com.au") 
Set objFolder = objFolder.Folders("Inbox").Folders("Actioned")

  Set db = DBEngine.OpenDatabase("S:\....\dbo.mailtrackinglog.accdb")
  Set rs = db.OpenRecordset("tbl_mailmovements", dbOpenTable)

For Each msg In olApp.ActiveExplorer.Selection
Set msg = olApp.ActiveExplorer.Selection(1)
rcvdTime= msg.ReceivedTime
mSubj = msg.Subject
mConvId= msg.ConversationID
mCateg = msg.Categories

Next msg

with rst
.AddNew
.Fields( )  =  rcvdTime
.Fields( )  = mSubj
.Fields( )  = mConvId
.Fields( )  = mCateg

.Update

End With
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
- I don't want this restriction. I would like to track every time an item is moved anywhere within the shared mail account.
The only way to do it in VBA is to watch each folder (added to app startup) - you can use a bunch of itemadds that point to the working macro - unless you want to do it hourly or on a schedule - then you could walk all folders looking for new items.

sub Items_ItemAdd(ByVal item As Object)
trackingmacro item
end sub

sub SentItems_ItemAdd(ByVal item As Object)
trackingmacro item
end sub

etc.

For Each msg In olApp.ActiveExplorer.Selection
Set msg = olApp.ActiveExplorer.Selection(1)

the first line says to do all items in the selection, but the second line says to do the first selection. if you are using itemadd, you'd only want to do the newly added item - remove those lines and either use set msg = item or change msg to item

how to use itemadd macros: How to use an ItemAdd Macro
 

novemberskye

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
the first line says to do all items in the selection, but the second line says to do the first selection. if you are using itemadd, you'd only want to do the newly added item - remove those lines and either use set msg = item or change msg to item

how to use itemadd macros: How to use an ItemAdd Macro

updated the 'for' statement to For x = 1 To myOlSel.Count
when I get to x = 2 I get an operation failed error

:(
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Do you want to run it on a selection? if so, use
For Each msg In olApp.ActiveExplorer.Selection

but get rid of this line -
Set msg = olApp.ActiveExplorer.Selection(1)

use with msg
'''do whatever
end with

next

The second macro on this page shows how to run macros on a selection : Working with All Items in a Folder or Selected Items

The problem is that you are mixing types of macros - if using an itemadd macro, you won't work with selections.
 

novemberskye

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
See updated code below
Been knocked to a different path with new issues I am unable to solve even after half a day of research and testing. :( :( :(

the issue I am having is getting looping through the selected / moved item in the Destination Folder by each EntryID in the Item_Add event

Code:
Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders("## Shrared email account here ##")
Set objFolder = objFolder.Folders("Inbox").Folders("## SUBFOLDER HERE ##")

Dim olApp As Outlook.Application

  Set olApp = Outlook.Application
  Set Items = objFolder.Items
  objFolder.Items.Sort "[LastModificationTime]", True  ''''<--- does not look like it's working
 
End Sub


---------------------------------------

Public Sub Items_ItemAdd(ByVal Item As Object)

  Dim myNameSpace As Outlook.NameSpace
  Dim objFolder As Outlook.MAPIFolder
  Dim myDestFolder As Outlook.Folder
  Dim myDestItems As Outlook.Items

    Set myNameSpace = Application.GetNamespace("MAPI")
    Set objFolder = myNameSpace.Folders("## Shrared email account here ##")
    'Set objFolder = objFolder.Folders("Inbox")
   
  Set myInbox = objFolder.Folders("Inbox")
  Set myItems = myInbox.Items
  Set myDestFolder = objFolder.Folders("Inbox").Folders("## SUBFOLDER HERE ##")
  Set myDestItems = myDestFolder.Items

  On Error GoTo 0

    Dim myOlApp As Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    Dim oMail As Outlook.MailItem
    Dim x, i, j As Integer
    Dim EntryIDCollection As String
    Dim myOlMItem As Outlook.MailItem
    Dim myOlAtts As Outlook.Attachments

    Set myOlApp = Outlook.Application
    Set myOlExp = myOlApp.ActiveExplorer
    Set myOlSel = myOlExp.Selection

 For Each Item In myOlSel
 EntryIDCollection = Item.EntryID & ","
 Next Item

  Set objNS = Application.GetNamespace("MAPI")
  strIDs = Split(EntryIDCollection, ",")
  For intX = 0 To UBound(strIDs)
  cMailItem = strIDs(intX)
  Set objEmail = objNS.GetItemFromID(cMailItem, Item.Parent.StoreID) '''''<----- ERROR HERE not able to get the mailitem by EntryID

   Set MyCurrentFolder = myOlExp.CurrentFolder
   Set msg = objEmail
   sTime = objEmail.ReceivedTime
   sSubj = objEmail.Subject
   sConID = objEmail.ConversationID
   sCat = objEmail.Categories
   sLMod = objEmail.LastModificationTime
   sFol = MyCurrentFolder
 

## 
Code here to upload mail item data to an access database
##

End sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
As Diane mentioned, you´re mixing different code. Use ItemAdd, if you want to handle on item added to a folder. Use the loop through selected items if you want to handle items selected by the user.

The For Each loop already loops through the items, giving you a ref on each selected item. Why do you first read only the IDs and then want to loop through that again?

Anyway, after the loop has finished the item variable is set to nothing. Thus you get an error trying to read a property of the item.

Since all items are from the same store, you can read the storeid from the folder object.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
C Tracking Calendar Changes? Using Outlook 1
L Attachment saving and tracking - PLEASE help! Outlook VBA and Custom Forms 5
R Outlook for Mac - Advanced Search and Tracking Sent Emails Using Outlook 0
H tracking time of emails Outlook VBA and Custom Forms 2
N Activity Tracking with Userform Outlook VBA and Custom Forms 4
S My Draft Emails Have Review Tracking Using Outlook 7
rino email campaign - tracking opened emails Outlook VBA and Custom Forms 6
C Problem tracking meeting responses Using Outlook 3
D tracking emails in outlook 2010 Using Outlook 1
C Rule to Handle Meeting Replies that updates tracking information Using Outlook 2
M Room Reservation request showed "None" in Tracking-View Tracking Status. Exchange Server Administration 0
C Tracking WHO deleted items in a Shared Mailbox Using Outlook 1
S How to remove tracking from printing - outlook 2010 Using Outlook 4
J Tracking sent emails Using Outlook 3
R Can't use "manage linking and tracking" BCM (Business Contact Manager) 10
P Tracking BCM Users BCM (Business Contact Manager) 1
K add an email to a business contact without linking/tracking sender BCM (Business Contact Manager) 0
K add an email to an account without linking and tracking the email address BCM (Business Contact Manager) 0
C Tracking Activities in Outlook Calendar Using Outlook 1
M Tracking Tasks Due Date Using Outlook 1
S Voting Buttons are Not Tracking Using Outlook 1
D Tracking meeting request responses in Outlook Outlook VBA and Custom Forms 16
B How to Remove Scheduling and Tracking on the Custom Meeting Form Outlook VBA and Custom Forms 4
F Tracking email in Outlook BCM (Business Contact Manager) 1
E BCM for Job Search Tracking? BCM (Business Contact Manager) 1
R Sent emails show iCloud mail account not the alias Using Outlook 1
D Advanced e-Mail search on from/to contact group only searches for first 20 contacts in group Using Outlook 0
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
P Importing other e-mail accounts into Outlook Using Outlook 1
lcarpay Stay in the mail folder pane after ctrl-1 Using Outlook 1
O Exchange Sync period only (e.g. last years mail) Using Outlook 0
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
M Convertor for Outlook Express Mail Store (.dbx) to Outlook Mail Store (.pst) Using Outlook 0
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
J E-mail held in Outbox while Minimized Using Outlook 3
G Forward email body to other mail list directly from Exchange server Exchange Server Administration 1
T Outlook creates a copie of every mail I send Using Outlook.com accounts in Outlook 4
N Please advise code received new mail Using Outlook 0
M Outlook 2010 How could I globally redesign an outlook template form/region/inspector template used to display mail lists or an individual mails? Outlook VBA and Custom Forms 0
A How to stop user form from disapearing once mail window is closed? Outlook VBA and Custom Forms 0
M Outlook, send to > mail recipient - results in plain text email Using Outlook 1
R How to Sync *all* Google Workspace Mail Folders with Outlook 2019 (MS365) Using Outlook 3
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
S Outlook email to configure setup for each mail Outlook VBA and Custom Forms 1
L Correct E-Mail Pulling Wrong Mail Using Outlook 5
S Outlook mail adressing stops after first match in GAL Using Outlook 0
P Outlook 2013 All imported Mail Rules in error when imported into new profile Using Outlook 5
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0

Similar threads

Top