从outlook-excel vba导入联系人组

从outlook-excel vba导入联系人组,excel,vba,outlook,Excel,Vba,Outlook,我有以下代码从Outlook导入所有联系人 Dim olApp As Outlook.Application Dim olNamespace As Outlook.Namespace Dim olFolder As Outlook.MAPIFolder Dim olConItems As Outlook.Items Dim olItem As Object Set olApp = New Outlook.Application Set olNamespace = olApp.GetNamespa

我有以下代码从Outlook导入所有联系人

Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olConItems As Outlook.Items
Dim olItem As Object
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderContacts)
Set olConItems = olFolder.Items


 'HERE IS THE PROBLEM I do not know how to do so that there are only contacts from my desired group in the olConItems collection
For Each olItem In olConItems
    If TypeName(olItem) = "ContactItem" Then
    'Do something - no problem I just do not want to post unnecessary code
    End If
Next olItem

我只需要导入属于某个联系人组的内容。如何获取联系人组属性?它是以某种方式暴露的吗?

从1循环到DistListItem.MemberCount并调用DistListItem.GetMember-它将返回收件人对象。如果收件人对象属性不够,请读取Recipient.AddressEntry以获取AddressEntry对象。

子例程从“MyGroupName”联系人组检索名称 并将其列在活动工作表中

Sub Get_Email_List()

    Dim I As Integer    
    Dim A1 As String
    Dim B() As String
    Dim WSN as String
    Dim Group as String

    Dim olApp As Outlook.Application
    Dim myNamespace As Object
    Dim myFolder As Object
    Dim myItem As Object
    Dim WordApp As Object

    Application.ScreenUpdating = False

    WSN = ActiveSheet.Name
    Group = "MyGroupName"

    Sheets(WSN).Select
    Selection.Clear
    Columns("A:D").Select
    Selection.NumberFormat = "@"
    Cells(1, 1).Select

    Set olApp = New Outlook.Application
    With olApp
        Set myNamespace = .GetNamespace("MAPI")
        Set myFolder = myNamespace.GetDefaultFolder(olFolderContacts)
        Set myItem = myFolder.Items(Group)
        For I = 1 To myItem.MemberCount
            Cells(I + 1, 1) = myItem.GetMember(I).Name
            Cells(I + 1, 3) = myItem.GetMember(I).Address
        Next I
    End With
    Set olApp = Nothing
    Set myNamespace = Nothing
    Set myFolder = Nothing
    Set myItem = Nothing

    Range("A1") = "Display Name"
    Range("B1") = "Last Name"
    Range("C1") = "Email Address"
    Range("D1") = "Composite Email Address"
    Range("A2:B" & I + 1).Select
    Selection.Cells.Replace What:="'", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    A1 = ""
    I = 2
    While Cells(I, 1) > ""
        If InStr(1, Cells(I, 1), ")") > 0 Then _
            Cells(I, 1) = Left(Cells(I, 1), InStr(1, Cells(I, 1), "(") - 2)

        B = Split(Cells(I, 1), " ")
        Cells(I, 2) = Trim(B(UBound(B, 1)))
        If I > 1 Then A1 = A1 & "; "
        A1 = A1 & Trim(Cells(I, 1))
        Cells(I, 4) = Cells(I, 1) & " <" & Cells(I, 3) & ">"
        I = I + 1
    Wend

    ActiveWorkbook.Worksheets(WSN).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(WSN).Sort.SortFields.Add Key:=Range("B2:B" & I), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets(WSN).Sort
        .SetRange Range("A2:D" & I)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    Columns("A:C").Select
    Selection.ColumnWidth = 28
    Columns("D:D").Select
    Selection.ColumnWidth = 48

    Range("A1:D1").Select
    Selection.Font.FontStyle = "Bold"
    Range("A2").Select
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    Range("A1").Select
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
子获取电子邮件列表()
作为整数的Dim I
尺寸A1为字符串
Dim B()作为字符串
将无线传感器网络设置为字符串
作为字符串的Dim组
Dim olApp作为Outlook.Application
将myNamespace设置为对象
将myFolder设置为对象
将myItem设置为对象
Dim WordApp作为对象
Application.ScreenUpdating=False
WSN=ActiveSheet.Name
Group=“MyGroupName”
工作表(WSN)。选择
选择,清楚
列(“A:D”)。选择
Selection.NumberFormat=“@”
单元格(1,1)。选择
Set olApp=newoutlook.Application
与奥拉普
设置myNamespace=.GetNamespace(“MAPI”)
设置myFolder=myNamespace.GetDefaultFolder(olFolderContacts)
设置myItem=myFolder.Items(组)
对于myItem.MemberCount的I=1
单元格(I+1,1)=myItem.GetMember(I).Name
单元格(I+1,3)=myItem.GetMember(I).地址
接下来我
以
设置olApp=Nothing
设置myNamespace=Nothing
设置myFolder=Nothing
设置myItem=Nothing
范围(“A1”)=“显示名称”
范围(“B1”)=“姓氏”
范围(“C1”)=“电子邮件地址”
范围(“D1”)=“复合电子邮件地址”
范围(“A2:B”和I+1)。选择
Selection.Cells.Replace What:=“”,Replacement:=”,LookAt:=xlPart,SearchOrder:=_
xlByRows,MatchCase:=False,SearchFormat:=False,ReplaceFormat:=False
A1=“”
I=2
而单元格(I,1)>“”
如果InStr(1,单元格(I,1),“)”)>0,则_
单元格(I,1)=左(单元格(I,1),仪表(1,单元格(I,1),“(”)-2)
B=拆分(单元格(I,1),“”)
单元(I,2)=微调(B(UBound(B,1)))
如果I>1,则A1=A1&“;”
A1=A1和修剪(单元(I,1))
单元(I,4)=单元(I,1)和“
I=I+1
温德
ActiveWorkbook.Worksheets(WSN).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(WSN).Sort.SortFields.Add Key:=范围(“B2:B”&I)_
SortOn:=xlSortOnValues,顺序:=XLASSENDING,数据选项:=xlSortNormal
使用ActiveWorkbook.Worksheets(WSN).Sort
.SetRange范围(“A2:D”和I)
.Header=xlGuess
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
申请
以
列(“A:C”)。选择
Selection.ColumnWidth=28
列(“D:D”)。选择
Selection.ColumnWidth=48
范围(“A1:D1”)。选择
Selection.Font.FontStyle=“Bold”
范围(“A2”)。选择
使用ActiveWindow
.SplitColumn=0
.SplitRow=1
以
ActiveWindow.FreezePanes=True
范围(“A1”)。选择
Application.CutCopyMode=False
Application.ScreenUpdating=True
端接头

事实并非如此。他们想在outlook内部通过outlook发送电子邮件。我想导出特定的组。他们建议只使用组的名称作为收件人。但这在我的情况下不起作用。您到底有什么问题?查找DL或导出其成员?您所说的“导出”到底是什么意思?以特定格式保存为文件?或只是读取属性?例如,导出的意思是通过向
/*Do something*/
部分添加代码将它们复制到我的工作表中。但这不是问题。我的问题是我不知道如何获取outlook中某个组中的联系人集合。假设我调用了组“客户”和我想将其用作每个循环的
源。但我不知道如何访问此特定集合