简体   繁体   中英

Problem in retrieving email data from (outlook) using excel/vba

I am trying to grab the following details from the sent items folder with specific subject line "Index Coverage". Details needs to be fetch from outlook to excel Sent by Sent to Subject Sent on (date) Subject email body using the below excel formula in the sheet with the following code added in outlooksession code module Index: =TRIM(MID(G2,SEARCH("Code",G2)+(8+LEN("Code")),20))

Our client: =LEFT(I2,FIND("on",I2)-1)

End client: =LEFT(K2,FIND(".",K2)-1)

Const strFilePath As String = "C:\Users\Public\Documents\Excel\OutlookMailItemsDB.xlsx"
Const strSubjectLineStartWith As String = ""
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
    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
            With CreateObject("Excel.Application").workbooks.Open(strFilePath)
                With .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
                .Close 1
            End With
            Set objItem = Nothing
        End If
    Next lngMailCounter
    If Not IsEmpty(strArray) Then
        Erase strArray
    End If
   
End Sub

Probelm in the code is - asof now i am only able to grab the following details - sent by, subject, sent on, Body, Index, our client, End client. I am not able to grab the recipient contact details also the excel sheet placed on desktop also needs to be saved and closed on its own so that next time it doesnt throw an error that excel is not closed.

Also, it should also consider the sent items folder with the following subject line: "Index Coverage", is there a way to add this clause.

one last thing to grab the details for the following fields - Index, our client and End client i am using excel formulas, is it possible to achieve this via vba?

在此处输入图像描述

在此处输入图像描述

First of all, creating a new Excel instance in the NewMailEx event handler each time a new email is received is not really a good idea. I'd suggest keeping a reference when the add-in works (like a singleton) to prevent any additional workload when receiving a new item.

Try to use the Recipients property of the MailItem class instead of using the To , Cc or Bcc fields. The Recipients collection returns a Recipients collection that represents all the recipients for the Outlook item. Use Recipients(index) where index is the name or index number, to return a single Recipient object. The name can be a string representing the display name, the alias, or the full SMTP email address of the recipient.

Finally, to process items added to the sent items folder you need to handle ItemAdd event which is fired when one or more items are added to the specified collection.

Public WithEvents myOlItems As Outlook.Items 

Public Sub Initialize_handler() 
 Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentItems).Items 
 
End Sub 

Private Sub myOlItems_ItemAdd(ByVal Item As Object) 
  ' your code for processing the Item object goes there
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM