VBA automates Outlook Mail Bug: put 2 strings in t

2019-08-06 08:55发布

After I run the following code, I will get to see the 1st part being displayed in my new email pop up window, but not the 2nd part. However, if I just click send and receive it, I will see 2 parts being displayed correctly.

Why? Why I didn't see the 2nd part in the new email pop up window BEFORE sending it?

Thanks!!

Code

    Dim OutApp As Object
Dim OutMail As Object

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = getConfig("MailTarget")
    .CC = ""
    .BCC = ""
    .Subject = "TEST"
    .HTMLBody = p1 & "<br><br>" & p2
    .Display   
End With
On Error GoTo 0

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing

Aside:

p1 = RangetoHTML(someRangeFromWorkBook1)
p2 = RangetoHTML(someRangeFromWorkBook2)

More Code:

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"

'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

1条回答
欢心
2楼-- · 2019-08-06 09:32

I agree with previous poster in that it seems to display both for me!

Initially I'd comment out the error trapping around the With OutMail block just incase some error is occuring that you are not seeing.

A couple of things I've found help with Outlook problems in the past:

  • Save each mail item.

So change maybe try the following:

With OutMail 
.To = getConfig("MailTarget") 
.CC = "" 
.BCC = "" 
.Subject = "TEST" 
.HTMLBody = p1 & "<br><br>" & p2 
.Save   '<<<<<<<<<<<
.Display    
End With
  • When I used to deal with lots of emails in the past I'd create a dynamic array of objects and as the emails get created I'd add then to the array and then at the very end of the procedure I'd empty the contents of the array and either display them or send them. Not sure of the exact syntax.
查看更多
登录 后发表回答