从outlook中的ReplyAll中删除特定地址

时间:2018-01-22 11:00:17

标签: vba outlook-vba

EDITED

我写了一个宏,在共享邮箱中找到一封电子邮件,然后回复它。问题是,在某些情况下,我想删除我的地址(共享@邮箱)或其他一些,我不知道该怎么做。我尝试了一些我发现但没有工作的方法。对不起这个基本问题。

    Const olFolderInbox = 6

    Sub Reminder()

    On Error Resume Next

    Dim olMail As Outlook.MailItem
    Dim myNamespace As Outlook.Namespace
    Dim myRecipient As Outlook.Recipient


    Set Outl = CreateObject("Outlook.Application")
    Set myNamespace = Outl.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("shared@inbox")


    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objInbox = objNamespace.GetSharedDefaultFolder(myRecipient, olFolderInbox)

    strFolderName = objInbox.Parent

    Set objMailbox = objNamespace.Folders(strFolderName)
    Set objFolder = objMailbox.Folders("Inbox").Folders("AAA").Folders("BBB")

    Set colItems = objFolder.Items

    Dim i As Long
    Dim Folder As Outlook.Folder


    i = 1
    For Each olMail In objFolder.Items





      If olMail.Subject = "AAA" + ActiveSheet.Range("D" & (ActiveCell.Row)) Then

      Set oReplyAll = olMail.ReplyAll

       oReplyAll.HTMLBody = "<BODY style=font-size:10pt;  font-family:Arial>Dear ,<br />  <br />" _
      & "Could you please remind the client to do something?<br />" _
      & "Thank you in advance.<br />" _
      <br /> </BODY>" _
       & oReplyAll.HTMLBody

      oReplyAll.CC = "xyz@xyz"

  '////////////////////////////////////////////////////////////////
  'EDIT
  '////////////////////////////////////////////////////////////////

    For j = 1 To oReplyAll.Recipients.Count
        With oReplyAll.Recipients(j)

        If .Name = "aaa@bbb" Then
           .Delete
            j = j - 1
    End If

    End With

    Next j


      oReplyAll.Display


    i = i + 1

        End If

    Next olMail


    End Sub

编辑:我在评论中添加了循环iSpain17 write3。没有改变。通常回复显示收件人在“收件人:”部分,尽管他应该被删除

2 个答案:

答案 0 :(得分:0)

已编辑:请注意,当您使用Recipients.Add方法时,您可以设置项目的Name属性。因此,当您使用for循环时,您必须针对Name属性进行测试。此外,删除项目会修改所有其他项目的索引,因此 i = i-1 。我试过这个,它对我有用。

For i = 1 To email.Recipients.Count

    With email.Recipients(i)

        If .Name = "address" Then
            .Delete
            i = i - 1
        End If

    End With

Next i

这将循环访问收件人中的每个收件人,如果它们与给定的emailmaddress匹配,则会从收件人中删除它们。

这可能不是确切的答案,因为我对outlook vba不太熟悉,但这是我将使用的逻辑。

答案 1 :(得分:0)

您想要匹配的文字很可能不是您认为的那样。

删除On Error Resume Next,以便修复此未经测试的代码中的任何错误。

使用此结构,您可以删除/移动多个匹配,因为索引未损坏。

For j = oReplyAll.Recipients.Count to 1 step -1

    With oReplyAll.Recipients(j)

        debug.print "text to match " & .name

        If .Name = "text to match" Then
            .Delete
        End If

    End With

Next j

使用此结构,您可以可靠地删除/移动一个匹配。

For j = 1 To oReplyAll.Recipients.Count

    With oReplyAll.Recipients(j)

        debug.print "text to match " & .name

        If .Name = "text to match" Then
            .Delete

            ' exit now,
            ' else next item is skipped,
            ' as it moves up into the position of the deleted item
            exit for 

        End If

    End With

Next j