为VBA脚本选择帐户

问题描述:

我已编写代码来限制收件人,抄送和密件抄送字段的地址数量。为VBA脚本选择帐户

唯一的问题是代码影响所有帐户,而不是我选择的特定帐户。

e.g: [email protected] 
    [email protected] 

我想这段代码只为“[email protected]”帐户,而不是“[email protected]”账户工作。但是代码在整个Outlook会话中运行。有没有选择在代码中选择帐户?

Private Sub Application_ItemSend(ByVal Element As Object, Cancel As Boolean) 
    Dim aaa() As String 
    Dim bbb() As String 
    Dim ccc() As String 
    aaa = Split(Element.To, ";") 
    bbb = Split(Element.CC, ";") 
    ccc = Split(Element.BCC, ";") 
    If (UBound(aaa) + 1) + (UBound(bbb) + 1) + (UBound(ccc) + 1) > 10 Then 
     MsgBox ("You have added too many recipients! Please contact your Administrator."), vbExclamation, "Authorization required!" 
     Cancel = True 
    End If 
End Sub 
+0

您的内容格式不正确的第一个编辑的格式 –

+0

@DenishParvadiaThank你丹麦语,我现在已经固定的格式。请看看这个。 –

+0

您的代码不正确请参见 –

我写了很多VBA,但没有用于Outlook,所以我在这里猜测了一下。怎么样在你之前插入任何东西:

If LCase(Environ("Username") & "@" & Environ("Userdnsdomain")) <> "[email protected]" Then Exit Sub 

您可以找到适用的帐户这样

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

Dim oAccount As account 

For Each oAccount In Session.Accounts 

    If oAccount = "[email protected]" Then 

     Dim aaa() As String 
     Dim bbb() As String 
     Dim ccc() As String 
     aaa = Split(Element.To, ";") 
     bbb = Split(Element.CC, ";") 
     ccc = Split(Element.BCC, ";") 

     If (UBound(aaa) + 1) + (UBound(bbb) + 1) + (UBound(ccc) + 1) > 1 Then 
      MsgBox ("You have added too many recipients! Please contact your Administrator."), vbExclamation, "Authorization required!" 
      Cancel = True 
     End If 

    End If 
Next 

ExitRoutine: 
    Set oAccount = Nothing 

End Sub