vba - 带有 Excel VBA 的 Outlook 2010 GAL

标签 vba excel outlook

我有以下代码可以从 Excel 中获取 Outlook 中的联系人:

Public Sub GetGAL()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFldr As Outlook.Items
Dim olContact As Outlook.ContactItem

Set olApp = CreateObject("Outlook.Application.14")
Set olNs = olApp.GetNamespace("MAPI")

Set olFldr = olNs.GetDefaultFolder(olFolderContacts).Items

For Each olContact In olFldr

Debug.Print olContact.FullName

Next olContact

End
End Sub

在这条线上失败了,说存在类型不匹配:
For Each olContact In olFldr

有人知道为什么吗?

此外,我如何访问 GAL 而不仅仅是我自己的联系人?

谢谢你的帮助。

编辑:这是我访问 addressEntry 和 ExchangeUser 的新代码,但是,还不是国家/地区字段:
Option Explicit

Public Sub GetGAL()

Application.ScreenUpdating = False

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olGAL As Outlook.addressEntries
Dim olAddressEntry As Outlook.addressEntry

Dim olUser As Outlook.ExchangeUser

Dim i As Long

'Dim sTemp As String

'Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(1)

Set olApp = CreateObject("Outlook.Application.14")
Set olNs = olApp.GetNamespace("MAPI")

Set olGAL = olNs.addressLists("Global Address List").addressEntries

'On Error Resume Next

For i = 1 To olGAL.Count

Set olAddressEntry = olGAL.Item(i)

If olAddressEntry.DisplayType = olRemoteUser Then

Set olUser = olAddressEntry.GetExchangeUser

'Debug.Print olUser.Name & ";" & olUser.StateOrProvince
'Debug.Print sTemp

'ws.Cells(i, 1) = olUser.Name
'ws.Cells(i, 2) = olUser.StateOrProvince

End If

Next i

End

Application.ScreenUpdating = True
End Sub

最佳答案

试试这个。尽管如果您的 GAL 中有大量条目,则需要一段时间才能完成,并且您可能必须增加 65000。

Sub tgr()

    Dim appOL As Object
    Dim oGAL As Object
    Dim oContact As Object
    Dim oUser As Object
    Dim arrUsers(1 To 65000, 1 To 2) As String
    Dim UserIndex As Long
    Dim i As Long

    Set appOL = CreateObject("Outlook.Application")
    Set oGAL = appOL.GetNameSpace("MAPI").AddressLists("Global Address List").AddressEntries

    For i = 1 To oGAL.Count
        Set oContact = oGAL.Item(i)
        If oContact.AddressEntryUserType = 0 Then
            Set oUser = oContact.GetExchangeUser
            If Len(oUser.lastname) > 0 Then
                UserIndex = UserIndex + 1
                arrUsers(UserIndex, 1) = oUser.Name
                arrUsers(UserIndex, 2) = oUser.PrimarySMTPAddress
            End If
        End If
    Next i

    appOL.Quit

    If UserIndex > 0 Then
        Range("A2").Resize(UserIndex, UBound(arrUsers, 2)).Value = arrUsers
    End If

    Set appOL = Nothing
    Set oGAL = Nothing
    Set oContact = Nothing
    Set oUser = Nothing
    Erase arrUsers

End Sub

关于vba - 带有 Excel VBA 的 Outlook 2010 GAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405567/

相关文章:

ms-access - 点击保存按钮后清除所有字段

excel - 非 Office 365 上 FILTER 功能的替代方案

c++ - 在 Excel 自动化中使用非本地化公式

vba - 按日期限制 Outlook 项目

excel - 如何将 Excel 工作表中的文本和图表复制到 Outlook 正文?

css - outlook.com 如何将 css 定位到特定类

excel - 在 VBA 中大写动态范围

vba - 与 web 下拉菜单交互,.dispatchEvent

vba - 复制列中除列标题之外的所有数据

vba - 事件工作表之后的 Excel 复制工作表