2017-05-27 148 views
-1

我想發送電子郵件(包含主題,正文...)到另一個電子郵件地址。 我嘗試下面的代碼,但沒有奏效:使用PowerPoint VBA發送電子郵件

Private Sub CommandButton1_Click() 

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

    strAddress = "[email protected]" 
    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 = "[email protected]" 
     .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兼容的代碼。

回答

2

使用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:郵寄地址-Link的
第三種方法是爲用戶提供一個鏈接,以點擊發送電子郵件:mailto:[email protected]?subject=xxx
用於這種方法的描述可以找到here(向下滾動到第三個選項)。

+0

我嘗試了現在的另一個代碼... 但我得到一個錯誤:運行 - yime錯誤'-2147220978(8004020e)' 而且它還說,發件人的地址被拒絕。 –

+0

如果您希望獲得任何幫助,您應該準確指定您嘗試過的其他代碼。理想情況下,您會將其添加到您的原始問題。 –

+0

我嘗試過這裏的代碼,但是ir太長了。這是第一個大代碼[https://www.rondebruin.nl/win/s1/cdo.htm]。 –

相關問題