通过Outlook从VBA自动发送电子邮件 [英] automatic email via Outlook from vba

查看:1070
本文介绍了通过Outlook从VBA自动发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力,并进行了一些Google搜索,以创建代码,该代码发送一封自动电子邮件,其中包含我的有效Excel工作表中的信息.该代码正确地准备了电子邮件,但是我仍然需要在电子邮件上按发送".我想自动发送它,所以我尝试在代码上添加.Send,但是没有用.

I have been working and did some Google searches to create code that sends an automatic email with information from my active Excel sheet. The code prepares the email properly, but I still need to press "Send" on the email. I would like to send it automatically, so I tried to add .Send on the code, but it didn't work.

此刻我的代码如下:

Sub CreateMail()

Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngCc As Range
Dim rngSubject As Range
Dim rngBody As Range

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With ActiveSheet
    Set rngTo = .Range("t2")
    Set rngSubject = .Range("t3")
    Set rngBody = .Range("a1:r35")
End With
rngBody.Copy

With objMail
    .To = rngTo.Value
    .Subject = rngSubject.Value
    .Display
End With
SendKeys "^({v})", True
.Send
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngCc = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing

End Sub

推荐答案

With objMail
    .To = rngTo.Value
    .Subject = rngSubject.Value
    .body = rngBody.Value
    .Send
End With

应该做到这一点. (注意:由于您似乎错过了邮件,因此我设置了邮件的正文.)

Should do the trick. (Note: I set the body of the message, since you seemed to have missed that.)

删除这些行:

SendKeys "^({v})", True
.Send

这篇关于通过Outlook从VBA自动发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆