【发布时间】:2017-12-18 10:05:44
【问题描述】:
我正在使用 Ron De Bruin 的以下代码添加一些文本和范围,从 excel 电子表格到电子邮件正文。我对vba的了解有限。我还想在电子邮件中添加默认签名。任何有关如何调整此代码以做到这一点的帮助将不胜感激。非常感谢。
Sub BOemail()
'
' BOemail Macro
'
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Username = Environ("username")
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Range("C1").Select
ActiveCell.FormulaR1C1 = "Available"
Range("A1").Select
Set rng = Nothing
' Only send the visible cells in the selection.
Set rng = Sheets("BOTable").Range("A1:D6").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = UserForm2.TextBox4.Text
.CC = ""
.BCC = ""
.Subject = "Backorder"
.HTMLBody = "Thank you for your order number" & " " & UserForm2.TextBox7.Value & "." & "<br><br>" & "Please see below as some of the items are currently out of stock. At this time, we are planning to hold your order until we can ship it to you complete. Please contact us if any of the items are available to ship and you want us to ship what we have now, and send the backordered items when they are available.<br><br>" & "We will keep you updated on your backorder." & RangetoHTML(rng)
.Attachments.Add "C:\Users\" & Username & "\Dropbox\Ample Supply Information\Ample Supply Company Line Card.pdf"
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
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
【问题讨论】:
-
ashleedawg- 当我转到链接并粘贴代码时,我收到错误“未定义子或函数”并且代码在 RangetoHTML 上停止。你能提供进一步的帮助吗?谢谢