loop through different filter options in Excel Macro

前端 未结 1 1532
余生分开走
余生分开走 2021-01-26 22:39

I am just wondering how to loop through different options in excel macro and perform the same operation.

My operation is to export IDs from excel to outlook distributio

相关标签:
1条回答
  • 2021-01-26 23:08
    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
    Dim i As Long, j as Long, teamNames() As String
    
    '''The Team Names are Stored in array '''''''''
    redim teamNames(1 to 3)
    teamNames() = Split("Red,Green,Blue", ",")
    '''''''''''''''''''''''''''''''''''''''''''''''
    Set objNameSpace = objOutlook.GetNamespace("MAPI")
    
    For j = LBound(teamNames) To UBound(teamNames) 
        Set objDistList = objOutlook.CreateItem(olDistributionListItem)
        Set objMail = objOutlook.CreateItem(olMailItem)
        Set objRecipients = objMail.Recipients
    
        ActiveSheet.Range("$A$1:$C$10").AutoFilter Field:=3, Criteria1:= _
        teamNames(j)
        objDistList.DLName = teamNames(j)
    
        For i = 2 To Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
            objRecipients.Add (Range("B" & i).Value)
        Next i
    
        objDistList.AddMembers objRecipients
        objDistList.Display
        objRecipients.ResolveAll
        Set objDistList = Nothing
        Set objMail = Nothing
        Set objRecipients = Nothing
    next j
    
    Set objOutlook = Nothing
    Set objNameSpace = Nothing
    
    
    End Sub
    

    You can try out the above I think it should work but didn't try it out. You should have a way to select the Distribution list name from the range in the spreadsheet or through a userinput rather than just counting from 1-3 IMHO. It is up to you though.

    Thanks

    0 讨论(0)
提交回复
热议问题