单击发送VBA生成的电子邮件时Outlook将关闭

时间:2016-10-17 18:47:58

标签: vba outlook access-vba ms-access-2013

在过去的几个月里,我一直在为自己的组织工作。我正在使用Office 2013.我已经从互联网上获取代码,使用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 

*公司电子邮件和公司名称不是字段。

1 个答案:

答案 0 :(得分:1)

Outlook的最后一个窗口(Explorer或Inspector)关闭后立即退出。

您可以使用MailItem.GetInspector并将返回的值存储在变量中,以防止Outlook关闭。