Outlook VBA 验证收件人 [英] Outlook VBA to verify recipient

查看:107
本文介绍了Outlook VBA 验证收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发送之前检查多个黑名单电子邮件地址的最佳方法是什么?

What is the best way to check for multiple black-list email addresses before sending?

我有几个电子邮件地址,作为项目的一部分,我不允许向其发送信息.我希望 Outlook 检查任何黑名单电子邮件地址,并在发送前通知我是否包含这些地址.下面是我发现要修改的代码

I have several email addresses I am not allowed to send information to as part of a project. I want Outlook to check for any of the black-list email addresses and notify me if they are included before sending. Below is the code I found to modify

例如,我的黑名单包括:bad@address.com"、worst@address.com"、evil@address.com"

For example, my black-list includes: "bad@address.com", "worst@address.com", "evil@address.com"

将这些地址放入下面代码的最佳方法是什么,以一种允许轻松更改黑名单中地址的方式会更好?

What is the best way to put these addresses into the code below, would be good for it to be in a way that allows for changing the addresses in the black-list easily?

这是我的代码的最新版本以及您的建议.不幸的是,它让我将电子邮件发送到清单上的地址.有什么建议吗?

So here is the latest version of my code with your suggestions. Unfortunately it lets me send the emails to the addresses on the Checklist. Any suggestions?

  Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

  Dim Recipients As Outlook.Recipients
  Dim recip As Outlook.Recipient
  Dim i
  Dim prompt As String


On Error Resume Next
 ' use lower case for the address
 ' LCase converts all addresses in the To field to lower case

  Checklist = "bad@address.com" & _
            "worst@address.com" & _
            "evil@address.com" '// , _ and so on

 Set Recipients = Item.Recipients
  For i = Recipients.Count To 1 Step -1
    Set recip = Recipients.Item(i)

 If InStr(LCase(recip), LCase(Checklist)) Then
      prompt$ = "You sending this to this to " & Item.To & ". Are you sure you want to send it?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If
  End If

Next i


End Sub

推荐答案

创建一个过程级变量 CheckList,它是黑名单电子邮件的 csv 列表.您可以在程序中将其初始化为硬分配或从其他数据源动态检索,例如sql服务器

Create a procedure level variable CheckList which is the list of csv of black listed emails. you can initialise this in the procedure as a hard assignment or dynamically retrieve from other data sources for e.g. sql server

Dim lbadFound  As Boolean
dim badAddresses as string
lbadFound = False

CheckList = "bad@address.com," & _
            "worst@address.com," &  _
            "evil@address.com" '// , _ and so on

    Set Recipients = Item.Recipients
    For i = Recipients.Count To 1 Step -1
      Set recip = Recipients.Item(i)

      If instr(1,lcase(CheckList),  LCase(recip)) >=1 Then 
          lbadFound = true
          badAddresses  = badAddresses  & recip & & vbcrlf 
      End If

    Next i

    If lbadFound Then
       prompt$ = "You sending this mail to one or more black listed email address(es)" & badAddresses & vbcrlf & " Are you sure you want to send it?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If
    End If

这篇关于Outlook VBA 验证收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆