フォルダーから電子メールの送信者の詳細(名前、役職、部署など)を取得しようとしています。私は私のアドレス帳のconactsのための必要な詳細を得ることができますが、私はGALの連絡先の詳細を取得していません。VBAからGALの電子メール送信者の詳細を取得する
私のコードは以下の通りです:
Public Sub DisplaySenderDetails()
Dim Sender As Outlook.AddressEntry
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim strColB, strColC, strColD, strColE, strColF, strColG As String
Dim objOL As Outlook.Application
Dim objItems As Outlook.Items
Dim objFolder As Outlook.MAPIFolder
Dim obj As Object
Dim objNS As Outlook.NameSpace
Dim olItem As Outlook.MailItem
Dim strdate As String
Dim oExUser As Outlook.ExchangeUser
Dim olGAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntries
' Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\test2.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
Set objNS = GetNamespace("MAPI")
Set olGAL = objNS.GetGlobalAddressList()
Set objFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Abc")
Set objItems = objFolder.Items
Set olEntry = olGAL.AddressEntries
For Each obj In objItems
With obj
Set Sender = obj.Sender
Set olItem = obj
If TypeName(obj) = "MailItem" Then
On Error Resume Next
Dim i As Long
For i = 1 To olEntry.Count
If olEntry.Item.Address = Sender.Address Then
Set oExUser = Sender.GetExchangeUser
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
rCount = rCount + 1
strdate = DateValue(olItem.ReceivedTime)
If strdate >= #7/1/2016# Then
strColB = Sender.Name
strColC = oExUser.JobTitle
strColD = oExUser.Department
strColE = oExUser.PrimarySmtpAddress
strColF = olItem.Subject
strColG = olItem.ReceivedTime
xlSheet.Range("B" & rCount) = strColB
xlSheet.Range("C" & rCount) = strColC
xlSheet.Range("D" & rCount) = strColD
xlSheet.Range("E" & rCount) = strColE
xlSheet.Range("F" & rCount) = strColF
xlSheet.Range("G" & rCount) = strColG
strColB = ""
strColC = ""
strColD = ""
strColE = ""
strColF = ""
trColG = ""
Else
Exit For
End If
End If
Next i
End If
End With
Next
Set obj = Nothing
Set objItems = Nothing
Set objFolder = Nothing
Set objOL = Nothing
私は次の関数を使用しています
コメントOn Error On Error GoTo ErrHandler。エラーがあればどうなりますか?デバッグの結果を提供します。 – niton