[英]Excel VBA Reorder Email Content
I have compiled the following code in VBA to send out an email. 我在VBA中编译了以下代码以发送电子邮件。 I want to send a simple message with a Range of cells in a table format. 我想以表格格式发送带有一系列单元格的简单消息。 The code below works. 以下代码有效。 But the output is flipped. 但是输出被翻转了。 I want the message text on top and the table on the bottom. 我希望消息文本在顶部,表格在底部。 (Please see image attached). (请参阅附图)。 Any time I try to move anything around I get a "424 Object Required" error. 每当我尝试移动任何东西时,我都会收到“424 Object Required”错误。
Please advise. 请指教。
Sub SendEmail()
Dim msg As String
Set mailApp = CreateObject("Outlook.Application")
Set mail = mailApp.createitem(olMailItem)
msg = "Good Morning Team, " & "<br><br>" _
& "Here is this week's Coffee Talk groups. Enjoy!" & "<br><br>"
With mail
.To = "someone@gmail.com"
.Subject = "Coffee Talk " & Date
.HTMLBody = msg
End With
mail.display
Set wEditor = mailApp.ActiveInspector.wordEditor
ThisWorkbook.Sheets("Groups").Range("A1:E4").Copy
wEditor.Application.Selection.Paste
End Sub
There is a Range to HTML function made by Ron de Bruin that would help you with your issue. 由Ron de Bruin制作的Range to HTML功能可以帮助您解决问题。 I have amended your code for his function to work: 我修改了你的代码以使其工作:
Sub SendEmail()
Dim msg As String
Set mailApp = CreateObject("Outlook.Application")
Set mail = mailApp.createitem(olMailItem)
Dim rng As Range
msg = "Good Morning Team, " & "<br><br>" _
& "Here is this week's Coffee Talk groups. Enjoy!" & "<br>"
Set rng = Sheets("Groups").Range("A1:E4").SpecialCells(xlCellTypeVisible)
With mail
.To = "someone@gmail.com"
.Subject = "Coffee Talk " & Date
.HTMLBody = msg & "<br>" & RangetoHTML(rng)
.display
End With
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
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 new workbook to past the data in
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
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a 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 RangetoHTML
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 we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.