使用PowerPoint VBA发送电子邮件
我想向另一个电子邮件地址发送电子邮件(主题、正文…)。 我尝试了以下代码,但无效:使用PowerPoint VBA发送电子邮件,vba,email,powerpoint,send,Vba,Email,Powerpoint,Send,我想向另一个电子邮件地址发送电子邮件(主题、正文…)。 我尝试了以下代码,但无效: 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),
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
Private子命令按钮1\u单击()
Dim ret作为布尔值
她穿得像绳子
Dim strMessage作为字符串
斯特拉德雷=”examplemail@gmail.com"
ret=sendmail(标签1.标题),strMessage)
标签1.Caption=ret
如果标签1.Caption=“True”,则
MsgBox“已发送邮件!”
ElseIf Label1.Caption=“False”然后
MsgBox“邮件未发送!”
如果结束
端接头
公共函数sendmail(strRecipient作为字符串,strSubject作为字符串,strBody作为字符串)作为布尔值
作为对象的Dim oApp
将oMail作为对象
呃,明白了
出错时继续下一步
设置oApp=GetObject(类:=“Outlook.Application”)
如果错误为0,则设置oApp=CreateObject(“Outlook.Application”)
呃,明白了
设置oMail=oApp.CreateItem(0)
与奥马尔
.Subject=strSubject
.To=strRecipient
“抄袭给自己
.CC=”youraddy@you.com"
.BodyFormat=1
.车身=车身
.发送
以
"清理",
设置oMail=Nothing
设置oApp=Nothing
“好吗?
如果Err=0,则sendmail=True,否则sendmail=False
端函数
代码最初取自
如果可能,我想要一个与大多数PC兼容的代码。使用Microsoft Outlook
要发送电子邮件,您需要在Microsoft Outlook中配置电子邮件帐户,因为您的代码正在使用Outlook发送电子邮件
Set-oApp=GetObject(类:=“Outlook.Application”)
备选方案1:SMTP
或者,您可以在VBA中设置SMTP连接,以便使用CDO通过外部邮件服务器发送电子邮件。有关VBA中CDO用法的更多信息,请参见(即使他们编写的代码是针对Excel的,您也可以将其用于PowerPoint)以及 这种方法的缺点是,SMTP登录凭据在VBA代码中可见。如果您计划与其他人共享此演示文稿,这可能是一个安全问题
备选方案2:Mailto链接
第三种方法是为用户提供一个链接,供其单击以发送电子邮件:
mailto:recipient@example.com?主题=xxx
可以找到此方法的说明(向下滚动到第三个选项) 我现在尝试另一个代码。。。但我得到了一个错误:运行yime error'-2147220978(8004020e)”,它还表示发件人的地址被拒绝。如果希望得到任何帮助,您应该准确地指定您尝试过的其他代码。理想情况下,您可以将其添加到原始问题中。我尝试在此处跳过代码,但ir太长。这是第一个大代码。任何帮助,请??您得到的错误是“发件人的地址被拒绝”。请检查代码中的SMTP设置。您很可能需要使用邮件服务器上的SMTP凭据进行身份验证才能发送电子邮件:
config.Fields(cdoSMTPAuthenticate).Value=cdoBasicconfig.Fields(cdoSendUserName).Value=“youUserName”
config.Fields(cdoSendPassword).Value=“yourPassword”
config.Fields(cdoSMTPServerPort).Value=xxx
配置字段(cdoSMTPUseSSL).Value=“true”