从Excel更新共享邮箱中的通讯组列表

从Excel更新共享邮箱中的通讯组列表,excel,vba,outlook,Excel,Vba,Outlook,我有以下宏,它在Excel中获取电子邮件地址列表,并在Outlook的“我的联系人”部分下创建/更新Outlook通讯组列表 如何调整此代码,使其在名为“共享测试”的共享邮箱中创建/更新联系人,而不是仅在我的邮箱中创建/更新联系人 Const DISTLISTNAME As String = "Test" Const olDistributionListItem = 7 Const olFolderContacts = 10 Sub test() 'Worksheet_Change(ByVal

我有以下宏,它在Excel中获取电子邮件地址列表,并在Outlook的“我的联系人”部分下创建/更新Outlook通讯组列表

如何调整此代码,使其在名为“共享测试”的共享邮箱中创建/更新联系人,而不是仅在我的邮箱中创建/更新联系人

Const DISTLISTNAME As String = "Test"
Const olDistributionListItem = 7
Const olFolderContacts = 10

Sub test() 'Worksheet_Change(ByVal Target As Range)

Dim outlook As Object ' Outlook.Application
Dim contacts As Object ' Outlook.Items
Dim myDistList As Object ' Outlook.DistListItem
Dim newDistList As Object ' Outlook.DistListItem
Dim objRcpnt As Object ' Outlook.Recipient
Dim arrData() As Variant
Dim rng As Excel.Range
Dim numRows As Long
Dim numCols As Long
Dim i As Long
Dim msg As String

msg = "Worksheet has been changed, would you like to update distribution list?"

  If MsgBox(msg, vbYesNo) = vbNo Then
    Exit Sub
  End If

  Set outlook = GetOutlookApp
  Set contacts = GetItems(GetNS(outlook))

  'On Error Resume Next
  Set myDistList = contacts.Item(DISTLISTNAME)
  On Error GoTo 0

  If Not myDistList Is Nothing Then
    ' delete it
    myDistList.Delete
  End If

    ' recreate it
    Set newDistList = outlook.CreateItem(olDistributionListItem)

    With newDistList
      .DLName = DISTLISTNAME
      .Body = DISTLISTNAME
    End With

    ' loop through worksheet and add each member to dist list
    numRows = Range("A1").CurrentRegion.Rows.Count - 1
    numCols = Range("A1").CurrentRegion.Columns.Count

    ReDim arrData(1 To numRows, 1 To numCols)

    ' take header out of range
    Set rng = Range("A1").CurrentRegion.Offset(1, 0).Resize(numRows, numCols)
    ' put range into array
    arrData = rng.Value

    ' assume 2 cols (name and emails only)
    For i = 1 To numRows
      'little variation on your theme ...
      Set objRcpnt = outlook.Session.CreateRecipient(arrData(i, 1) & "<" & arrData(i, 2) & ">")
      'end of variation
      objRcpnt.Resolve
      newDistList.AddMember objRcpnt
    Next i

    newDistList.Save
    'newDistList.Display

End Sub

Function GetOutlookApp() As Object
  'On Error Resume Next
  Set GetOutlookApp = CreateObject("Outlook.Application")
End Function

'To My Contacts
Function GetItems(olNS As Object) As Object
Set GetItems = olNS.GetDefaultFolder(olFolderContacts).Items
End Function

Function GetNS(ByRef app As Object) As Object
  Set GetNS = app.GetNamespace("MAPI")
End Function
Const DISTLISTNAME As String=“Test”
Const OldDistributionListItem=7
常数olFolderContacts=10
子测试()'工作表\u更改(ByVal目标作为范围)
将outlook设置为对象的outlook.Application
将联系人设置为对象的Outlook.Items
将myDistList设置为对象“Outlook.DistListItem”
将newDistList设置为对象“Outlook.DistListItem”
Dim objRcpnt作为对象的Outlook.Recipient
Dim arrData()作为变量
尺寸为Excel.Range
暗淡的珠子长得一样
暗淡的numCols与长
我想我会坚持多久
作为字符串的Dim msg
msg=“工作表已更改,是否要更新通讯组列表?”
如果MsgBox(msg,vbYesNo)=vbNo,则
出口接头
如果结束
设置outlook=GetOutlookApp
设置联系人=GetItems(GetNS(outlook))
'出现错误时,请继续下一步
设置myDistList=contacts.Item(DISTLISTNAME)
错误转到0
如果不是myDistList,那么什么都不是
“删除它
myDistList.Delete
如果结束
“重新创造它
Set newDistList=outlook.CreateItem(oldDistributionListItem)
与newDistList
.DLName=DISTLISTNAME
.Body=DISTLISTNAME
以
'循环浏览工作表并将每个成员添加到dist列表
numRows=范围(“A1”).CurrentRegion.Rows.Count-1
numCols=范围(“A1”).CurrentRegion.Columns.Count
ReDim arrData(1到numRows,1到numCols)
'将收割台移出范围
设置rng=Range(“A1”).CurrentRegion.Offset(1,0)。调整大小(numRows,numCols)
'将范围放入阵列
arrData=rng.值
'假设2列(仅限姓名和电子邮件)
对于i=1到numRows
“你的主题有点变化。。。
设置objRcpnt=outlook.Session.CreateRecipient(arrData(i,1)和“”)
“变异的结束
objRcpnt.Resolve
newDistList.AddMember对象JRCPNT
接下来我
newDistList.Save
'newDistList.Display
端接头
函数GetOutlookApp()作为对象
'出现错误时,请继续下一步
设置GetOutlookApp=CreateObject(“Outlook.Application”)
端函数
“给我的联系人
函数GetItems(OLN作为对象)作为对象
设置GetItems=olNS.GetDefaultFolder(olFolderContacts.Items)
端函数
函数GetNS(ByRef应用程序作为对象)作为对象
设置GetNS=app.GetNamespace(“MAPI”)
端函数

引用非默认文件夹的一种方法是使用
.CreateRecipient

代码中的函数似乎不能提高效率

Option Explicit

Const DISTLISTNAME As String = "Test"
Const olDistributionListItem = 7
Const olFolderContacts = 10

Sub test()

    Dim outlook As Object       ' Outlook.Application
    Dim olNs As Object          ' Outlook.Namespace

    Dim shareRecipient As Object            ' outlook.recipient
    Dim sharedMaiboxContacts As Object      ' outlook.Folder
    Dim sharedMaiboxContactsItems As Object ' outlook.items

    Dim myDistList As Object    ' Outlook.DistListItem
    Dim newDistList As Object   ' Outlook.DistListItem

    Dim objRcpnt As Object      ' outlook.recipient

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

    ' Enter mailbox name in "sharedMailboxName"
    ' Email address is not as useful. Even if invalid, cannot fail a resolve

    Set shareRecipient = olNs.CreateRecipient("sharedMailboxName")

    shareRecipient.Resolve

    If shareRecipient.Resolved Then

        Set sharedMaiboxContacts = olNs.GetSharedDefaultFolder(shareRecipient, olFolderContacts)
        sharedMaiboxContacts.Display
        Set sharedMaiboxContactsItems = sharedMaiboxContacts.Items

        ' This is a valid use of On Error Resume Next
        '  to bypass a known possible error
        '
        ' Before finalizing the code, test with this commented out
        '  where you think there should not be an error
        '  or you may bypass unknown errors, for example when the syntax is wrong.
        On Error Resume Next

        ' A possible known error occurs if the list does not exist.
        ' myDistList can remain "Nothing" instead of causing an error.
        Set myDistList = sharedMaiboxContactsItems.Item(DISTLISTNAME)

        ' Turn the bypass off. / Turn normal error handling on.
        ' Place it as soon as possible after On Error Resume Next
        On Error GoTo 0

        If Not myDistList Is Nothing Then
            ' delete it
            myDistList.Delete
        End If

        ' Add to non default folders
        Set newDistList = sharedMaiboxContactsItems.Add(olDistributionListItem)

        With newDistList
            .DLName = DISTLISTNAME
            .body = DISTLISTNAME
        End With

        Debug.Print olNs.CurrentUser

        ' Test with yourself
        Set objRcpnt = olNs.CreateRecipient(olNs.CurrentUser)

        objRcpnt.Resolve

        If objRcpnt.Resolved Then
            newDistList.AddMember objRcpnt
            newDistList.Display
        Else
            Debug.Print objRcpnt & " not resolved."
        End If

    Else

        Debug.Print shareRecipient & " not resolved."

    End If

End Sub

第一个好的步骤是在下一步恢复时消除所有的
,并向我们展示代码产生的实际错误。我一直不明白人们为什么这么做。为什么您不想知道代码中的错误在哪里?嗨,谢谢您的回复。但是,上面的代码不会出错,并且可以完美地将联系人上载到我邮箱上的“我的联系人”中。但是我正在寻找一种方法,让它将联系人上传到一个共享邮箱,所以我想知道如何调整代码来实现这一点。如果它工作得很好,为什么不删除这些表达式呢?这只会让我们手头的事情变得更容易,所以我们不必预测你的代码会有什么遗传性的错误是的,我会感谢你。好建议。我是VBA新手,刚刚在另一个论坛上找到了上面的内容,但我想知道这是如何适应的。感谢
关于错误的回复下一步
被严重误用,因此大多数时候来自Rawrplus的建议都是有效的。在错误恢复下一步时首次使用
是有益的。你会发现你必须保持它未注释。注意只绕过您知道的错误,并根据需要处理或不处理错误。第二次执行“错误恢复下一步”
既没有好处也没有坏处。