VBA自动化Outlook Mail Bug:将两个字符串放在HTMLBody中 [英] VBA automates Outlook Mail Bug: put 2 strings in the HTMLBody

查看:1178
本文介绍了VBA自动化Outlook Mail Bug:将两个字符串放在HTMLBody中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码后,我将看到第一部分显示在我的新电子邮件弹出窗口中,但不是第二部分。但是,如果我只是点击发送和接收,我将看到正确显示2部分。

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?

谢谢!!

代码

    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)

更多代码:

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


推荐答案

我同意以前的海报,似乎为我显示!

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

最初,我会注释掉在OutMail块附近的错误,只是发生一些您没有看到的错误。

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

我发现有几件事情有助于Outlook中的Outlook问题:

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


  • 保存每个邮件项。

所以更改也许尝试以下:

So change maybe try the following:

With OutMail 
.To = getConfig("MailTarget") 
.CC = "" 
.BCC = "" 
.Subject = "TEST" 
.HTMLBody = p1 & "<br><br>" & p2 
.Save   '<<<<<<<<<<<
.Display    
End With




  • 当我过去处理大量的电子邮件,我会创建一个动态数组的对象,并且随着电子邮件的创建,我会添加到数组,然后在程序的最后,我将清空的内容数组,并显示或发送它们。不确定语法。

  • 这篇关于VBA自动化Outlook Mail Bug:将两个字符串放在HTMLBody中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆