Reputation: 1
I have a macro to get in excel all the emails from a shared mailbox In my shared inbox I have created 2 new fields where we enter some information related to the emails received. I have named them Client and Oper.
Can you help me please with a suggestion? How can I see the field property in order to get it in VBA?
The macro looks like this:
Option Explicit
Sub getDataFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim objOwner As Outlook.Recipient
Dim i As Integer
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set objOwner = OutlookNamespace.CreateRecipient("[email protected]")
objOwner.Resolve
If objOwner.Resolved Then
Set Folder = OutlookNamespace.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("email_receipt_date").Value Then
Range("email_sender").Offset(i, 0) = OutlookMail.SenderName
Range("email_sender").Offset(i, 0).Columns.AutoFit
Range("email_sender").Offset(i, 0).VerticalAlignment = xlTop
Range("email_subject").Offset(i, 0) = OutlookMail.Subject
Range("email_subject").Offset(i, 0).Columns.AutoFit
Range("email_subject").Offset(i, 0).VerticalAlignment = xlTop
'Range("email_client").Offset(i, 0) = OutlookMail.Client
'Range("email_client").Offset(i, 0).Columns.AutoFit
'Range("email_client").Offset(i, 0).VerticalAlignment = xlTop
'Range("email_oper").Offset(i, 0) = OutlookMail.Oper
'Range("email_oper").Offset(i, 0).Columns.AutoFit
'Range("email_oper").Offset(i, 0).VerticalAlignment = xlTop
Range("email_date").Offset(i, 0) = OutlookMail.ReceivedTime
Range("email_date").Offset(i, 0).Columns.AutoFit
Range("email_date").Offset(i, 0).VerticalAlignment = xlTop
Range("email_categories").Offset(i, 0) = OutlookMail.Categories
Range("email_categories").Offset(i, 0).Columns.AutoFit
Range("email_categories").Offset(i, 0).VerticalAlignment = xlTop
Range("email_flag_status").Offset(i, 0) = OutlookMail.FlagStatus
Range("email_flag_status").Offset(i, 0).Columns.AutoFit
Range("email_flag_status").Offset(i, 0).VerticalAlignment = xlTop
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
Upvotes: 0
Views: 203
Reputation: 644
You can utilize the UserProperties
of the MailItem.
For example OutlookMail.UserProperties("Oper").Value
will return the value of that property field you created.
Note that it WILL cause an error it that field is not populated so it is suggested to use Find
beforehand with an IF check to ensure you don't throw an exception.
If Not(OutlookMail.UserProperties.Find("Oper", True) Is Nothing) Then
'Do stuff with OutlookMail.UserProperties("Oper").Value
End If
Upvotes: 0