【问题标题】:Send an Email using PowerPoint VBA使用 PowerPoint VBA 发送电子邮件
【发布时间】:2017-10-28 04:30:29
【问题描述】:

我想向另一个电子邮件地址发送一封电子邮件(带有主题、正文...)。 我尝试了以下代码,但没有成功:

Private Sub CommandButton1_Click()

    Dim ret As Boolean
    Dim strAddress As String
    Dim strMessage As String

    strAddress = "examplemail@gmail.com"
    ret = SendEMail(strAddress, (Label1.Caption), strMessage)
    Label1.Caption = ret

    If Label1.Caption = "True" Then
        MsgBox "Mail sent!"
    ElseIf Label1.Caption = "False" Then
        MsgBox "Mail not sent!"
    End If

End Sub


Public Function SendEMail(strRecipient As String, strSubject As String, strBody As String) As Boolean

    Dim oApp As Object
    Dim oMail As Object

    Err.Clear
    On Error Resume Next

    Set oApp = GetObject(Class:="Outlook.Application")
    If Err <> 0 Then Set oApp = CreateObject("Outlook.Application")
    Err.Clear
    Set oMail = oApp.CreateItem(0)
    With oMail
        .Subject = strSubject
        .To = strRecipient
        'copy to self
        .CC = "youraddy@you.com"
        .BodyFormat = 1
        .Body = strBody
        .Send
    End With

    'cleanup
    Set oMail = Nothing
    Set oApp = Nothing

    'All OK?
    If Err = 0 Then SendEMail = True Else SendEMail = False

End Function

代码最初取自here

如果可能的话,我想要一个与大多数 PC 兼容的代码。

【问题讨论】:

    标签: vba email powerpoint send


    【解决方案1】:

    使用 Microsoft Outlook
    要发送电子邮件,您需要在 Microsoft Outlook 中配置一个电子邮件帐户,因为您的代码使用 Outlook 发送电子邮件。

    Set oApp = GetObject(Class:="Outlook.Application")


    备选方案 1:SMTP
    或者,您可以在 VBA 中设置 SMTP 连接,以使用 CDO 通过外部邮件服务器发送电子邮件。更多关于在 VBA 中使用 CDO 的信息可以找到here(即使他们编写的代码是为 Excel 编写的,您也可以将其用于 PowerPoint)和here

    这种方法的缺点是 SMTP 登录凭据在 VBA 代码中可见。如果您打算与其他人共享此演示文稿,这可能是一个安全问题。


    备选方案 2:Mailto-Link
    第三种方法是向用户提供一个链接以供点击以发送电子邮件:mailto:recipient@example.com?subject=xxx
    这种方法的描述可以在here 找到(向下滚动到第三个选项)。

    【讨论】:

    • 我现在尝试了另一个代码...但是我收到一个错误:Run-yime error '-2147220978 (8004020e)' 它还说发件人的地址被拒绝了。
    • 如果您希望得到任何帮助,您应该准确指定您尝试过的其他代码。理想情况下,您会将其添加到您的原始问题中。
    • 我试图在这里跳过代码,但 ir 太长了。这是第一个大码here
    • 有什么帮助吗??
    • 你得到的错误是“发件人的地址被拒绝”。请检查代码中的 SMTP 设置。您很可能需要在邮件服务器上使用您的 SMTP 凭据进行身份验证才能发送电子邮件:config.Fields(cdoSMTPAuthenticate).Value = cdoBasicconfig.Fields(cdoSendUserName).Value = "youUserName"config.Fields(cdoSendPassword).Value = "yourPassword"config.Fields(cdoSMTPServerPort).Value = xxxconfig.Fields(cdoSMTPUseSSL).Value = "true"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2011-09-28
    • 2016-04-15
    • 1970-01-01
    • 2015-05-03
    • 2021-11-21
    • 2015-06-07
    相关资源
    最近更新 更多