Outlook VBA 更改在项目加载时使用帐户发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18342667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:52:16  来源:igfitidea点击:

Outlook VBA change Send using account when item load

vbaoutlook-2010

提问by BlueBeardo

I'm trying to change the From account when an item loads in Outlook 2010 using VBA. I have two accounts, a gmail account and a POP3.

我正在尝试使用 VBA 在 Outlook 2010 中加载项目时更改发件人帐户。我有两个帐户,一个 gmail 帐户和一个 POP3。

When replying, replying all, and forwarding Outlook defaults to the account that the email was received through. If I receive an email through Gmail, I want to reply with a POP3 account. Even though my default account is the POP3 account, Outlook changes it to Gmail.

回复、全部回复和转发时,Outlook 默认使用接收电子邮件的帐户。如果我通过 Gmail 收到一封电子邮件,我想用 POP3 帐户回复。即使我的默认帐户是 POP3 帐户,Outlook 也会将其更改为 Gmail。

This is what I have so far. Unfortunately I get the error: Run-time error '-6936698555 (d6a70005)': You don't have appropriate permission to perform this operation.

这是我到目前为止。不幸的是,我收到错误消息:运行时错误“-6936698555 (d6a70005)”:您没有执行此操作的适当权限。

Private Sub Application_ItemLoad(ByVal Item As Object)
    Set myObj = GetCurrentItem()
    If TypeName(myObj) = "MailItem" Then
        Set OutApp = CreateObject("Outlook.Application")
        Set oMail = OutApp.CreateItem(olMailItem)
        Dim oAccount As Outlook.Account

        Set oMail = myObj

        oMail.SendUsingAccount = oMail.SendUsingAccount.Session.Accounts.Item(1)
    End If
End Sub


Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing
End Function

Am I going about this the right way by doing it on Item load? Why don't I have permission to change the sender? Is it because VB didn't create the email?

我在项目加载时以正确的方式解决这个问题吗?为什么我没有更改发件人的权限?是不是因为 VB 没有创建电子邮件?

回答by codeonabike

I'm late to this party, but I was trying to do something very similar and ran across your question/code. I managed to get it working.

我参加这个聚会迟到了,但我试图做一些非常相似的事情并遇到了你的问题/代码。我设法让它工作。

The problem is that GetCurrentItem() is returning the mail item from your inbox (or wherever). What you need to modify is the new message created by hitting "reply".

问题是 GetCurrentItem() 正在从您的收件箱(或任何地方)返回邮件项目。您需要修改的是通过点击“回复”创建的新消息。

I took your code and modified it. I've added a reply event that changes the SendUsingAccount property of the response. The ItemLoad event checks the 'To' property of the current mail item to decide whether or not to set the reply event.

我拿了你的代码并修改了它。我补充说,改变的SendUsingAccount财产的回复事件的响应。ItemLoad 事件检查当前邮件项的“To”属性来决定是否设置回复事件。

Public WithEvents SecondAcctMsg As MailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
    Set myObj = GetCurrentItem()
    If TypeName(myObj) = "MailItem" Then
        Select Case myObj.To
            Case "<relevant email address>"

                Set SecondAcctMsg = myObj

        End Select
    End If
End Sub


Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"

            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"

            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing
End Function

Private Sub SecondAcctMsg _Reply(ByVal Response As Object, Cancel As Boolean)

   ' Change Accounts index to relevant account
    Response.SendUsingAccount = Application.Session.Accounts(2)

End Sub

回答by Graham Anderson

I think the mailitem should be set using the following:

我认为应该使用以下设置mailitem:

Set oMail = OutApp.CreateItem(olMailItem)

and setting the application should be done either using:

并设置应用程序应该使用:

Set OutApp = CreateObject("Outlook.Application")

if outlook isn't open or the following if it is:

如果 Outlook 未打开或以下内容:

Set OutApp = GetObject(, "Outlook.Application")

I haven't used SendUsingAccount, I'm guessing you would need to set the account to your alternative account. I have used SendOnBehalfOfName maybe this would work e.g.

我没有使用过 SendUsingAccount,我猜您需要将该帐户设置为您的备用帐户。我使用过 SendOnBehalfOfName 也许这会起作用,例如

oMail.SentOnBehalfOfName = "Your POP3 account name"
oMail.Send