我有一个VBA代码,它在Excel中复制一个范围并粘贴到Outlook电子邮件的正文中。代码适用于我的几个同事计算机,但不适用于我的计算机。代码可以创建.temp文件,填充To:,CC:和Subject但是没有任何内容出现在正文中。我正在尝试粘贴HTML。我认为这是一个设置或类似的东西,但我不知道从哪里开始。对此的任何帮助将不胜感激。
Sub CreateEmail()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
datestr = Date
sbj = "***"
toStr = "****"
Ccstr = "*****"
Dim rng As Range
Set rng = Nothing
Set rng = Range("Flash")
On Error Resume Next
With OutMail
.To = toStr
.Display
.CC = Ccstr
.BCC = ""
.Subject = sbj & datestr
.HTMLBody = RangetoHTML(rng)
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
Application.ScreenUpdating = False
TempFile = Environ$("temp") & "/" & "temp" & ".htm"
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
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
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=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
Application.ScreenUpdating = True
结束功能
答案 0 :(得分:0)
我选择使用此方法。这会粘贴一系列excel,但不会粘贴HTML版本。我不需要使用HTML。这样做效果一样好,但没有颜色等。
Sub SendFlash()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
Set rng = Range("K4:L8").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
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
toStr = "*"
Ccstr = "*"
sbj = "Tran"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = toStr
.CC = Ccstr
.BCC = ""
.Subject = sbj & Date
.Body = TableRangeToTabDelim(rng)
.display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function TableRangeToTabDelim(TableRange As Range) As String
Dim TableCols As Integer
Dim TableRows As Integer
Dim ReturnString As String
TableCols = TableRange.Columns.Count
TableRows = TableRange.Rows.Count
i = 1
j = 1
Do While i <= TableRows
Do While j <= TableCols
ReturnString = ReturnString & TableRange.Cells(i, j).Value
If j <> TableCols Then
ReturnString = ReturnString & Chr(9)
End If
j = j + 1
Loop
j = 1
If i <> TableRows Then
ReturnString = ReturnString & Chr(10)
End If
i = i + 1
Loop
TableRangeToTabDelim = ReturnString
End Function