2016-10-17 35 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才会关闭。 –

相关问题