Outlook 2003 VBA在发送时检测选定的帐户

时间:2011-09-30 08:25:41

标签: vba outlook outlook-vba outlook-2003

是否可以通过Outlook 2003的Application_ItemSend VBA函数检测正在发送电子邮件的帐户?这些帐户是独立计算机上的POP3 / SMTP,而不是 MAPI或基于Exchange。

我尝试使用“Outlook Redemption”(http://www.dimastr.com/redemption/),但我找不到任何可以告诉我电子邮件通过哪个帐户发送的属性/方法。< / p>

我无需修改/选择发送的帐户,只需检测即可。

3 个答案:

答案 0 :(得分:1)

我找到了一种查找帐户名称的方法,感谢this link提供了选择特定帐户的代码。

使用此代码作为基础,我创建了一个简单的GetAccountName函数,它完全按照我的需要执行。

修改:只有当您使用Word作为编辑器时,以下内容才有效。

Private Function GetAccountName(ByVal Item As Outlook.MailItem) As String
    Dim OLI As Outlook.Inspector
    Const ID_ACCOUNTS = 31224

    Dim CBP As Office.CommandBarPopup

    Set OLI = Item.GetInspector
    If Not OLI Is Nothing Then
        Set CBP = OLI.CommandBars.FindControl(, ID_ACCOUNTS)
        If Not CBP Is Nothing Then
            If CBP.Controls.Count > 0 Then
                GetAccountName = CBP.Controls(1).Caption
                GoTo Exit_Function
            End If
        End If
    End If
    GetAccountName = ""

Exit_Function:
    Set CBP = Nothing
    Set OLI = Nothing
End Function

答案 1 :(得分:0)

这是一个尝试:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
   Msgbox(Item.SendUsingAccount.DisplayName)
End Sub

这将显示当前发送帐户的显示名称 如果这还不够,您可以尝试Item.sendUsingAccount var。

的其他属性

答案 2 :(得分:0)

在Outlook 2003中,您需要使用“兑换”中的RDOMail对象来访问邮件项目的“帐户”属性。这是一些代码,用于针对发件箱中的所有项目将SendAccount从默认帐户更改为OL配置文件中的另一个帐户。可以通过编写一个帐户选择子例程来改进它,该例程会读取OL Profile中的帐户并将其显示为列表以供用户选择。在提供的代码中,对新的发送帐户进行了硬编码。

Sub ChangeSendAccountForAllItems()
    On Error Resume Next
    Dim oOutlook As Application
    Dim olNS As Outlook.NameSpace
    Dim sOrigSendAccount As String
    Dim sNewSendAccount As String
    Dim iNumItemsInFolder As Integer
    Dim iNumItemsChanged As Integer
    Dim i As Integer

    Dim rRDOSession As Redemption.RDOSession
    Dim rRDOFolderOutbox As Redemption.RDOFolder
    Dim rRDOMail As Redemption.RDOMail

    'Create instance of Outlook
    Set oOutlook = CreateObject("Outlook.Application") 
    Set olNS = Application.GetNamespace("MAPI")

    'Create instance of Redemption
    Set rRDOSession = CreateObject("Redemption.RDOSession") 
    rRDOSession.Logon

    'Set a new Send Account (using Redemption)
    'Change this to any SendAccount in your Profile
    sNewSendAccount = "ThePreferredSendAccountNameInTheProfile"       
    Set rRDOAccount = rRDOSession.Accounts(sNewSendAccount)

    Response = MsgBox("New Send Account is: " & sNewSendAccount & vbCrLf & _
        vbCrLf, _
        vbOK + vbInformation, "Change SendAccount for All Items")

    'Get items in Outbox folder (value=4) (using Redemption)
    Set rRDOFolderOutbox = rRDOSession.GetDefaultFolder(olFolderOutbox)
    Set rRDOMailItems = rRDOFolderOutbox.Items
    iNumItemsInFolder = rRDOFolderOutbox.Items.Count
    iNumItemsChanged = 0

    'For all items in the folder, loop through changing Send Account (using Redemption)
     For i = 1 To iNumItemsInFolder
        Set rRDOItem = rRDOMailItems.Item(i)
        rRDOItem.Account = rRDOAccount
        rRDOItem.Save
        iNumItemsChanged = iNumItemsChanged + 1

        '3 lines below for debugging only
        'Response = MsgBox("Item " & iNumItemsChanged & " of " & iNumItemsInFolder & " Subject: " & vbCrLf & _
        '            rRDOItem.Subject & vbCrLf, _
        '            vbOK + vbInformation, "Change SendAccount for All Items")

    Next

    Response = MsgBox(iNumItemsChanged & " of " & iNumItemsInFolder & " items " & _
        "had the SendAccount changed to " & sNewSendAccount, _
        vbOK + vbInformation, "Change SendAccount for All Items")

    Set olNS = Nothing
    Set rRDOFolderOutbox = Nothing
    Set rRDOMailItems = Nothing
    Set rRDOItem = Nothing
    Set rRDOAccount = Nothing
    Set rRDOSession = Nothing

End Sub