2016-10-17 64 views
1

最近幾個月,我一直在爲自己的組織開發數據庫。我使用的是Office 2013.我已將互聯網上的代碼通過Outlook發送給我的客戶。但不管我編輯多少代碼,問題仍然存在。當點擊發送VBA生成的電子郵件時Outlook將關閉

我已經設置了.Display屬性,以便用戶在發送前可以看到消息。問題是它顯示我的消息,但是當我點擊發送按鈕時,它關閉了前景。但是,如果我使用郵件的.Send屬性,則沒有問題。

用途:我發送html電子郵件,我已將html代碼保存在我的表格中,以便點擊按鈕即可獲取模板。哪些可供用戶進一步編輯。備選方案非常感謝! :D

Private Sub CmdEmail_Click() 

Dim oApp As Object 
Dim oMail As Object 
Dim olAccount As Object 
Dim olAccounts As Object 
Dim olAccountTemp As Object 
Dim vallL As String 
Dim foundAccount As Boolean 
Dim strFrom As String 

On Error Resume Next 
Set oApp = CreateObject("Outlook.Application") 
Set oMail = oApp.CreateItem(olMailItem) 
Set olAccount = oApp.Account 
Set olAccountTemp = oApp.Account 

strFrom = CompanyEmail 
foundAccount = False 
Set olAccounts = oApp.Application.Session.Accounts 
For Each olAccountTemp In olAccounts 
Debug.Print olAccountTemp.SmtpAddress 
If (olAccountTemp.SmtpAddress = strFrom) Then 
    Set olAccount = olAccountTemp 
    foundAccount = True 
    Exit For 
End If 
Next 

Set oMail.SendUsingAccount = olAccount 
If foundAccount Then 
Debug.Print "ACCT FOUND!" 
With oMail 
    .BodyFormat = olFormatHTML 'Set body format to HTML 
    vallL = DLookup("[Memo]", "HtmlEmailT", "[ID] = 1") & "rs!CliName" 
    vallL = vallL & DLookup("[Memo]", "HtmlEmailT", "[ID] = 2") & "rs!InvoiceId" 
    vallL = vallL & DLookup("[Memo]", "HtmlEmailT", "[ID] = 3") & "rs!BalDue" 
    vallL = vallL & DLookup("[Memo]", "HtmlEmailT", "[ID] = 4") & "rs!InvoiceDate" 
    vallL = vallL & DLookup("[Memo]", "HtmlEmailT", "[ID] = 5") & "rs!InvTotal" 
    vallL = vallL & DLookup("[Memo]", "HtmlEmailT", "[ID] = 6") 
    .HTMLBody = vallL 
    .SendUsingAccount = olAccount 
    .SentOnBehalfOfName = """CompnayName"" <CompanyEmail>" 
    .Display 
    '.Send 
End With 
Else 
    Debug.Print "No acct found" 
    MsgBox "The chosen email is not signed in!!" & vbCrLf & "Please sign in first" 
End If 

Set oApp = Nothing 
Set oMail = Nothing 
Set olAccounts = Nothing 
Set olAccount = Nothing 
Set olAccountTemp = Nothing 

End Sub 

*公司的電子郵件和公司名稱不是字段。

+0

當您運行此代碼時,Outlook是否已打開,還是讓代碼打開Outlook? – Chrismas007

+0

我很抱歉不提這件事。只有在Outlook未打開時纔會發生。如果它是開放的,那就沒有問題了。 –

+0

錯誤恢復時刪除下一步。不要在整個代碼中使用它。一旦使用原因過去了,你需要一個On Error Goto 0.在這裏使用它似乎不是一個理由。 – niton

回答

1

Outlook的最後一個窗口(Explorer或Inspector)關閉後即退出。

您可以使用MailItem.GetInspector並將返回的值存儲在變量中以防止Outlook關閉。

+0

嗨@dmitry,感謝您的幫助,請您再解釋一下,我如何使用它來克服我的情況。我沒有得到如何使用mailitem.getinspector。將值存儲在變量中以防止Outlook關閉後,我該怎麼辦? –

+0

只需將值存儲在一個變量中 - 直到對其某個檢查器的引用處於活動狀態時,Outlook纔會關閉。 –

相關問題