如何从Word VBA设置Outlook从电子邮件地址



我正在尝试使用Word VBA将文档发送给电子邮件收件人。在大多数情况下,这并不困难。到目前为止,我有这段代码:

With oItem
'Set the recipient for the new email
.To = "person1@mail.com"
'Set the recipient for a copy
.CC = "ccperson@mail.com"
'Set the subject
.Subject = "Blah blah"
End With

我的问题是我在Outlook中配置了多个发件人电子邮件地址,默认情况下Outlook选择了错误的一个。

有没有办法使用上述方法指定发件人电子邮件地址?不用说,用于指定发件人地址(.From = me@wherever.com(的直观代码行不起作用。谢谢。

更新:

我终于让我的代码在下面使用peakpeak和Dimitry的建议修改后工作。我的更改是

1( 包含对 Microsoft Outlook 16 对象库的引用,以便我可以访问Outlook.MailItem数据类型。邮件可以使用上面的代码(没有参考(发送,但总是使用错误的发件人地址发送。

2( 将邮件项目声明为Outlook.MailItem。这似乎使SentOnBehalfOfName领域成为可能。

3(在SentOnBehalfOfName字段中使用了我想要的发件人:电子邮件地址。

这是工作代码:

Dim MAPIMailItem As Outlook.MailItem
Set MAPIMailItem = olkApp.CreateItem(olMailItem)  'Create a new mail message
With MAPIMailItem
.BodyFormat = olFormatPlain
.to = strTo
' SentOnBehalfOfName sets the From field on my machine,
' AFTER I declared MAPIMailItem as Outlook.MailItem
.SentOnBehalfOfName = "fromAddress@foo.com"
.Subject = strSubject
.body = strBody
.attachments.Add strAtt
'.send
.Display
End With

我使用以下代码:

Dim WantedAccount as String ' Set to preferred account name
Set MAPISession = objOutlook.Application.Session     'Get the MAPI Outlook session
Set MAPIMailItem = objOutlook.CreateItem(olMailItem)  'Create a new mail message
With MAPIMailItem
For Each Account In MAPISession.Accounts
If Account = WantedAccount Then
.SendUsingAccount = Account
Exit For
End If
Next

如果通过 Exchange 帐户发送邮件,请设置MailItem.SentOnBehalfOfName属性(假定您有权代表指定邮箱进行发送(。如果通过 POP3/SMTP 帐户发送,请设置MailItem.SendUsingAccount属性。

相关内容

最新更新