【问题标题】:Sending email through MS Access VBA / Outlook, choosing sending profile通过 MS Access VBA / Outlook 发送电子邮件,选择发送配置文件
【发布时间】:2013-08-01 07:55:42
【问题描述】:

我正在从另一个问题 (MS Access VBA) 中查看这个 sn-p 代码:https://stackoverflow.com/a/17975507/1085885

目前,此代码仅在我在 Outlook 打开时运行它时才有效。这段代码有没有办法“打开 Outlook”然后运行所有发送代码?

其次,如何选择从哪个 Outlook 配置文件发送?我可以访问几个不同的个人资料,它是从我的主要收件箱发送的,但我希望它来自我的第二个收件箱。

【问题讨论】:

标签: vba ms-access outlook


【解决方案1】:

您需要登录到指定的配置文件。创建 Outlook 应用程序实例后

Set oApp = CreateObject("Outlook.application")

添加如下内容:

set oNS = oApp.GetNamespace.Logon
oNS.Logon("MyProfileName")

请注意,如果 Outlook 已经在运行,Logon 将不会执行任何操作。您将需要使用扩展 MAPI(C++ 或 Delphi 或像 Redemption (RDOSession.Logon) 之类的 MAPI 包装器来登录到指定的配置文件。

为什么不使用单个配置文件并创建多个帐户?然后,您可以设置MailItem.SendUsaingAccount 属性来指定特定帐户。

【讨论】:

  • 如何使用 SendUsingAccount 属性?我确实创建了 MailItem(请参阅代码)
【解决方案2】:

试试这个方法。

Private Sub Command1_Click()

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next


'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    'Outlook wasn't running, start it from code
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
    'Set the recipient for the new email
   .To = "receiver@gmail.com"

    .Send
End With

If bStarted Then
'    'If we started Outlook from code, then close it
    oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing



End Sub

【讨论】:

    猜你喜欢
    • 2021-05-11
    • 2020-05-16
    • 2013-10-15
    • 2020-05-30
    • 2011-09-28
    • 2016-05-11
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多