Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

excel - RangetoHTML keeping TempWB open

I am using Ron de Bruin's RangetoHTML without modification to copy a range to an html email body. On my pc in Excel 2010, it works as intended. On a colleague's pc running Excel 2016, it appears to take the range, create the temp workbook and stop. The temp workbook includes the correct range, but it never makes it to the email body, nor does the code close out and delete the temp workbook. It still correctly populates the to/cc and subject but the .HTMLbody is blank. Any ideas?

Code below:

...
On Error Resume Next

    Dim objOutlook as Object
    Set objOutlook = CreateObject("Outlook.Application")

    Dim objEmail as Object
    Set objEmail = objOutlook.CreateItem(olMailItem)

    With objEmail
        .To = ""
        .CC = ""
        .BCC = ""
        .Attachments.Add (attachmentPath)
        .Subject = strSubject
        .HTMLbody = RangetoHTML(rng) & signature
        .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 paste 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 the function
Kill TempFile

Set ts= Nothing
Set fso = Nothing
Set TempWB = Nothing

End Function
question from:https://stackoverflow.com/questions/65876016/rangetohtml-keeping-tempwb-open

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...