2013-05-31 286 views
1

我有以下代碼發送Outlook郵件。但是,當前景關閉時,這不起作用。
Sub DraftMail(emailAddr, strBody, strSub)
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
End If Set OutMail = OutApp.CreateItem(0)
從excel發送outlook郵件vba

On Error Resume Next 
With OutMail 
    .To = "" 
    .CC = "" 
    .BCC = emailAddr 
    .Subject = strSub 
    .HTMLBody = strBody 
    .Send 'or use .Display 
    .ReadReceiptRequested = True 
End With 
On Error GoTo 0 

Set OutMail = Nothing 
Set OutApp = Nothing 

End Sub

任何人可以幫助我如何使它甚至工作時的前景被關閉?

回答

0

試試這個代碼: (未測試)

Sub SendMail() 

    Dim iMsg As Object 
    Dim iConf As Object 
    Dim Flds As Variant 


    Set iMsg = CreateObject("CDO.Message") 
    Set iConf = CreateObject("CDO.Configuration") 

    iConf.Load -1 
    Set Flds = iConf.Fields 

    With Flds 
     .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<server>" 
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
     .Update 
    End With 

    With iMsg 

     Set .Configuration = iConf 
     .To = "[email protected]" 
     .From = "[email protected]" 
     .Subject = "MIS Reports" & " " & Date & " " & Time 
     .TextBody = "Link to Currency Data :" & vbNewLine & "<" & myDest & ">" 
     .Send 
    End With 

    Set iMsg = Nothing 
    Set iConf = Nothing 

End Sub 
+0

桑托斯感謝您的答覆。 但我只需要通過Outlook發送郵件。 您能否修改我的代碼以使其工作 – Tjs

+0

@Tjs您需要輸入服務器詳細信息,更改爲,從電子郵件ID&希望它應該工作,即使對於Outlook。 – Santosh

0

首先你有任何錯誤在那裏處理?例如,如果你調用getObject並關閉它,你應該得到一個錯誤?

所以大多數人使用的方式是調用get對象,如果那個錯誤,那麼他們知道outlook關閉,他們創建一個新的實例。

如果你想成爲非常精確的錯誤代碼號爲429在這裏看到,例如驗證碼Link to previous question.

爲了讓您一開始,這也應該工作

On Error Resume Next 

Set OutApp = GetObject(, "Outlook.Application") 
If OutApp Is Nothing Then 
    Set OutApp = CreateObject("Outlook.Application") 
End If 

一旦這方面的工作,那麼你可以刪除「On Error Resume Next」,並捕獲特定錯誤429(如果需要),然後您知道該錯誤是因爲Outlook未運行。

0

最簡單的做法是構建一個mailto://網址並通過shell command運行它。

您必須爲URL encode標題和正文才能正常顯示。

例如(粘貼在運行命令在Windows中):

mailto://[email protected]?subject=New%20Email&body=This%20is%20the%20message%20body.

Result

相關問題