1

我正在使用下面的宏从 Excel 发送电子邮件。我已经包含了处理电子邮件正文的“Ron De Bruin”方法(Function RangetoHTML(rng As Range))。然而,在函数结束时,我得到了一个运行时错误('438'),它把我带到了子(.html = rangeto html(rngtext))中的以下行。我已经检查了代码中的所有内容,并且我相信我拥有所有必需的“Dim”等。任何帮助将不胜感激。

Sub Email_set_up()
Dim valdate As String
Dim oOApp As New Outlook.Application
Dim oOmail As Outlook.MailItem
Dim rng As Range, rngText As Range
Dim strbody As String
Dim SigString As String
Dim Signature As String
Set oOApp = CreateObject("Outlook.Application")
Set oOmail = oOApp.CreateItem(olMailItem)
valdate = Format(Sheets("E-Mail").Cells(4, 2), "mm/dd/yyyy")

SigString = Environ("appdata") & _
 "\Microsoft\Signatures\CK_Sign.txt"

If Dir(SigString) <> "" Then
    Signature = GetBoiler(SigString)
Else
    Signature = ""
End If

Set rngText = Sheets("E-Mail").Range("A18:A29")

Sheets("E-Mail").Activate
    With oOmail
        For Each c In Range(Sheets("E-Mail").Range("B6"), Sheets("E-Mail").Range("B9"))
            .Recipients.Add c
        Next
        .CC = Sheets("E-Mail").Range("B11")
        .Subject = Sheets("E-Mail").Range("B16") & valdate
        .HTML = RangetoHTML(rngText)
        .Display
    End With
End Sub

Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.ReadAll
ts.Close
End Function

Function RangetoHTML(rng As Range)
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"

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
End Function
4

1 回答 1

1

我想你想要:

.HTMLBody = RangetoHTML(rngText)

我不相信.HTML是有效的。

于 2012-11-27T16:25:52.970 回答