【问题标题】:Remove line breaks before and after table in HTML mail body in outlook 365删除 Outlook 365 中 HTML 邮件正文中表格前后的换行符
【发布时间】:2021-06-05 09:24:59
【问题描述】:

我正在尝试使用 vba 从 Outlook 365 发送电子邮件。在运行编码时,我在插入表格之前和之后以及签名之后都会出现换行符。除此之外,我的编码运行良好。

谁能帮我找到删除不需要的换行符的解决方案

我从当前编码中得到的结果:

编码的预期结果:

下面是我创建的代码(参考 rondebruin)。

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String
Dim SigString As String
Dim Signature As String


StrBody = "<BODY style = Font-size:11pt; font-fanily:calibri>" & _
      "Hi All" & "," & "<br>Attached is the list of opportunities which are created last week." & " 
<br><br>" & _
      "Please let me know if there is any concern or query." & "<br><br> <b> OPE details :</b>"


SigString = Environ("appdata") & _
            "\Microsoft\Signatures\sig.htm"

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

On Error Resume Next
  
Workbooks.Open Filename:="C:\Work\Projects\Data\Data.xlsx"
Sheets("OPE details Pivot").PivotTables(1).TableRange1.Select

Set rng = Nothing
On Error Resume Next
Set rng = Selection.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

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
OutMail.SentOnBehalfOfName = ""
     .to = ""
     .CC = ""
     .Importance = 2
     .BCC = ""
     .Subject = "mail"
     .HTMLbody = StrBody & RangetoHTML(rng) & Signature & .HTMLbody
    .Display
End With
On Error GoTo 0

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

   
Set OutMail = Nothing
Set OutApp = Nothing

End Sub

下面是我通过引用rondebruin创建的rangetoHTML函数的编码。

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.Select
     Cells.EntireRow.AutoFit
     Cells.EntireColumn.AutoFit
    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

最后以下是我也通过使用rondebruin的引用创建的签名编码

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

【问题讨论】:

    标签: html excel vba outlook


    【解决方案1】:

    在查看您的代码时,似乎在您调用 RangetoHtml(rng) 函数时会出现额外的行

    .HTMLbody = StrBody & RangetoHTML(rng) & Signature & .HTMLbody
    

    请您检查一下,或者分享我可以查看的代码。

    【讨论】:

    • 技巧 我已经编辑了我的问题并分享了 RangetoHtml(rng) 函数和 GetBoiler 函数的编码。请查看并帮助纠正此问题。如果您需要我的其他详细信息,请告诉我..
    【解决方案2】:

    感谢您分享信息。您的代码 (RangetoHtml) 看起来不错。

    我怀疑,您所指的转换为 html 的范围包括一个额外的行。 您能否检查一下您是否在该范围内选择了额外的行。我认为在下面的代码中,选择了额外的一行。您可以通过单独运行以下代码来检查这一点:

    Sheets("OPE details Pivot").PivotTables(1).TableRange1.Select
    
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    
    Rng.copy
    

    【讨论】:

    • 我已经通过运行给定的代码检查了完整的范围,范围内没有额外的行。即使我给出了特定的范围,邮件正文中仍然有额外的行。有没有任何编码可以删除线..
    【解决方案3】:

    在 RangetoHTML 函数中的“TempWB.Close”行之前添加以下行解决了我的问题。

    RangetoHTML = Replace(RangetoHTML, "<!--[if !excel]>&nbsp;&nbsp;<![endif]-->", "")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多