我一直在努力并进行一些Google搜索,以创建代码,该代码会发送一封自动电子邮件,其中包含来自我的有效Excel工作表的信息代码正确地准备了电子邮件,但我仍然需要按"发送"在电子邮件上。我想自动发送,所以我尝试在代码上添加.Send
,但它没有用。
目前我的代码看起来像这样:
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
答案 0 :(得分:1)
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.body = rngBody.Value
.Send
End With
应该做的伎俩。 (注意:我设置了邮件正文,因为您似乎错过了。)
删除这些行:
SendKeys "^({v})", True
.Send
答案 1 :(得分:0)
Outlook对象模型提供了三种使用项主体的方法:
您可以在MSDN中的Chapter 17: Working with Item Bodies中详细了解所有这些方法。
完成工作的最简单方法是使用Word对象模型来修改邮件正文。例如:
mail.GetInspector().WordEditor
然后,您可以使用Word对象模型来处理邮件正文。
最后,您需要调用Send方法来提交邮件以供传输提供程序处理。
答案 2 :(得分:0)
这是完整的 代码 ,对我有用。
Option Explicit
Sub CreateMail()
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngCc As Range
Dim rngBCC 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 rngCc = .Range("T2")
Set rngBCC = .Range("T2")
Set rngSubject = .Range("T3")
Set rngBody = .Range("A1:R35")
End With
rngBody.Copy
With objMail
.To = rngTo.Value
.CC = rngCc.Value
.BCC = rngBCC.Value
.Subject = rngSubject.Value
.HTMLBody = RangetoHTML(rngBody)
ActiveWorkbook.Save
.Send
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngCc = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
' Copy the range and create a workbook to receive the data.
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
Cells(1).Select
Cells(1).Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
' Publish the sheet to an .htm file.
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
' Read all data from the .htm file into the RangetoHTML subroutine.
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
' Close TempWB.
TempWB.Close savechanges:=False
' Delete the htm file.
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function