更新宏以在后台打开并保存Outlook通讯组列表

时间:2015-10-30 19:26:15

标签: excel vba excel-vba outlook

我想将Excel中的联系人导出到Outlook通讯组列表。我能够用给定的代码做到这一点。我每次关闭Excel工作表时都会运行宏。这显式打开了Outlook,我们每次都必须选择Save and Close。以下是我的代码:

Public Sub DistributionList()

Dim objOutlook As New Outlook.Application
Dim objNameSpace As Outlook.Namespace
Dim objDistList As Outlook.DistListItem
Dim objMail As Outlook.MailItem
Dim objRecipients As Outlook.Recipients


Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objDistList = objOutlook.CreateItem(olDistributionListItem)
Set objMail = objOutlook.CreateItem(olMailItem)
Set objRecipients = objMail.Recipients
objDistList.DLName = "Green"

Dim ws As Worksheet
Set ws = Sheets("Sheet1")

Dim lRow As Long
lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
ws.Range("$A$1:$C" & lRow).AutoFilter Field:=3, Criteria1:="1"

Dim rRng As Range, cel As Range
Set rRng = ws.Range("B2:B" & lRow).SpecialCells(xlCellTypeVisible)


For Each cel In rRng
   objRecipients.Add cel
Next

objDistList.AddMembers objRecipients
objDistList.Display
objRecipients.ResolveAll

Set objOutlook = Nothing
Set objNameSpace = Nothing
Set objDistList = Nothing
Set objMail = Nothing
Set objRecipients = Nothing

ws.AutoFilterMode = False

End Sub

我想知道是否有任何方法可以在宏中包含代码以避免“保存并关闭”窗口打开。

有没有办法可以修改代码,以便在背景中打开分发列表并自行保存?

1 个答案:

答案 0 :(得分:0)

删除objDistList.Display并添加:

objDistList.Save
objDistList.Close

这应该达到你的目标。的问候,