0

我有一个 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

结束功能

4

1 回答 1

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
于 2012-11-20T17:11:15.093 回答