Hi,
My requirement is, when i receive a new mail in outlook, it should trigger a macro which saves the new mail info (Sender address, date received, subject etc..) in an excel sheet. I have a macro code which does just that, but it only works for personal mailbox. I want to mimic the same steps for shared mailbox. Here is the code which works perfectly fine for personal mailbox,
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim varArray As Variant
Dim strSub As String
Dim strBody As String
Dim strArray() As String
Dim lngLoop As Long
Dim objItem As Object
Dim lngMailCounter As Long
Dim objMItem As MailItem
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
strArray = Split(EntryIDCollection, ",")
For lngMailCounter = LBound(strArray) To UBound(strArray)
Set objItem = Session.GetItemFromID(strArray(lngMailCounter))
If TypeName(objItem) = "MailItem" And InStr(1, objItem.Subject, strSubjectLineStartWith) And InStr(1, objItem.Body, "") Then
Set objMItem = objItem
Set xlApp = GetObject(, "Excel.Application")
Set wb = xlApp.Workbooks("test2.xlsx")
With wb.Sheets(1)
With .Cells(.Rows.Count, 1).End(-4162)(2).Resize(1, 7)
.Value = Array(objMItem.SenderEmailAddress, objMItem.To, objMItem.CC, objMItem.BCC, objMItem.Subject, objMItem.ReceivedTime, objMItem.Body)
End With
End With
Set objItem = Nothing
End If
Next lngMailCounter
If Not IsEmpty(strArray) Then
Erase strArray
End If
End Sub
I am not well-versed with codes. So please reply with the codes.
Thanks for your time.
My requirement is, when i receive a new mail in outlook, it should trigger a macro which saves the new mail info (Sender address, date received, subject etc..) in an excel sheet. I have a macro code which does just that, but it only works for personal mailbox. I want to mimic the same steps for shared mailbox. Here is the code which works perfectly fine for personal mailbox,
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim varArray As Variant
Dim strSub As String
Dim strBody As String
Dim strArray() As String
Dim lngLoop As Long
Dim objItem As Object
Dim lngMailCounter As Long
Dim objMItem As MailItem
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
strArray = Split(EntryIDCollection, ",")
For lngMailCounter = LBound(strArray) To UBound(strArray)
Set objItem = Session.GetItemFromID(strArray(lngMailCounter))
If TypeName(objItem) = "MailItem" And InStr(1, objItem.Subject, strSubjectLineStartWith) And InStr(1, objItem.Body, "") Then
Set objMItem = objItem
Set xlApp = GetObject(, "Excel.Application")
Set wb = xlApp.Workbooks("test2.xlsx")
With wb.Sheets(1)
With .Cells(.Rows.Count, 1).End(-4162)(2).Resize(1, 7)
.Value = Array(objMItem.SenderEmailAddress, objMItem.To, objMItem.CC, objMItem.BCC, objMItem.Subject, objMItem.ReceivedTime, objMItem.Body)
End With
End With
Set objItem = Nothing
End If
Next lngMailCounter
If Not IsEmpty(strArray) Then
Erase strArray
End If
End Sub
I am not well-versed with codes. So please reply with the codes.
Thanks for your time.