[英]Problem in retrieving email data from (outlook) using excel/vba
我正在嘗試從具有特定主題行“索引覆蓋率”的已發送項目文件夾中獲取以下詳細信息。 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( "代碼",G2)+(8+LEN("代碼")),20))
我們的客戶:=LEFT(I2,FIND("on",I2)-1)
終端客戶端:=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
代碼中的問題是-到目前為止,我只能獲取以下詳細信息-發送者,主題,發送者,正文,索引,我們的客戶,最終客戶。 我無法獲取收件人的聯系方式,並且放置在桌面上的 excel 表也需要自行保存和關閉,以便下次它不會拋出 excel 未關閉的錯誤。
此外,它還應該考慮具有以下主題行的已發送郵件文件夾:“索引覆蓋率”,有沒有辦法添加此子句。
最后一件事是獲取以下字段的詳細信息 - 索引、我們的客戶端和最終客戶端我正在使用 excel 公式,是否可以通過 vba 實現這一點?
首先,每次收到新的 email 時,在NewMailEx
事件處理程序中創建一個新的 Excel 實例並不是一個好主意。 我建議在加載項工作(如單例)時保留參考,以防止在接收新項目時產生任何額外的工作量。
嘗試使用MailItem
class 的Recipients
屬性,而不是使用To
、 Cc
或Bcc
字段。 Recipients集合返回一個Recipients
集合,該集合代表 Outlook 項目的所有收件人。 使用Recipients(index)
,其中 index 是名稱或索引號,返回單個Recipient
object。 名稱可以是表示顯示名稱、別名或收件人的完整 SMTP email 地址的字符串。
最后,要處理添加到已發送項目文件夾的項目,您需要處理在將一個或多個項目添加到指定集合時觸發的ItemAdd
事件。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.