如何使用Excel UserForm自动发送Outlook电子邮件?

时间:2015-02-23 15:39:17

标签: email excel-vba outlook-vba sendkeys vba

感谢您对此提供任何帮助。我有一个用户表单我收集了用户的标准,然后当他们点击提交时打开Outlook并将该数据通过电子邮件发送给我。

我有2个问题。首先,当我尝试使用SENDKEYS方法时,我遇到了拼写检查功能,阻止电子邮件实际发送,而无需用户通过它。有没有办法绕过拼写检查并发送电子邮件?

其次,我无法在不使用SENDKEYS的情况下自动发送电子邮件,但我确信有更好的方式发送电子邮件而不是使用TAB键操作窗口。

Private Sub SubmitButton_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strBody, RequestName, ProductName, Month, TestName, Summary As String

If Me.RequesterNameTxt.Value <> "" And Me.ProductCombo.Value <> "" And Me.MonthCombo.Value <> "" And Me.TestNameCombo <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

RequestName = Me.RequesterNameTxt.Value
ProductName = Me.ProductCombo.Value
Month = Me.MonthCombo.Value
TestName = Me.TestNameCombo.Value
Summary = Me.SummaryTxt.Value


strBody = "<HTML><BODY>"
strBody = "Requester Name: " & RequestName & "<BR>" & "Product Name: " & ProductName & "<BR>" & "Month: " & Month & "<BR>" & _
"Test Name: " & TestName & "<BR>" & "<BR>" & "Summary of Request: " & Summary
strBody = strBody & "</BODY></HTML>"

On Error Resume Next
With OutMail
    .To = "example@gmail.com;"
    .CC = ""
    .bcc = ""
    .Subject = "QA Service Request"
    .htmlBody = strBody
    .send 'This fixed my issue. I had this as .Display which opens email up and doesn't send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Application.SendKeys ("%s")
    Else: MsgBox "Please fill out all form data before submitting request. Thank you!"
End If

End Sub

1 个答案:

答案 0 :(得分:1)

您需要使用Outlook项目的Send方法。 Send方法使用为会话指定的默认帐户发送项目。在配置文件中定义了多个Microsoft Exchange帐户的会话中,添加到配置文件的第一个Exchange帐户是主Exchange帐户,也是会话的默认帐户。要指定另一个帐户来发送项目,请将SendUsingAccount属性设置为所需的Account对象,然后调用Send方法。

另外,我建议您使用“收件人”属性来添加收件人。该属性返回一个Recipients集合,该集合表示Outlook项目的所有收件人。