多行文本框将Excel错误粘贴到Outlook HTML中

时间:2019-04-02 08:17:24

标签: excel vba outlook

我有一个带有文本框的VBA用户窗体,该文本框使用Enter接受多行。 之后,我的代码会将插入的数据从此TextBox复制到文本为WordWrap的Excel单元格(接受多行)。 最后,在RangetoHTML中使用“ Ron de Bruin”代码,将Excel表单复制到HTML电子邮件(https://www.rondebruin.nl/win/s1/outlook/bmail2.htm)中。 除了此TextBox之外,所有带有一行代码的excel单元格都已正确复制到HTML电子邮件中,因为我注意到它包含Multiline或WordWrap,并且在每行末尾添加了CrLf代码(见下文)。

TextBox value:

line1
line2

line3
line4


Excel Cell result where TextBox is copied:

line1
line2

line3
line4


Excel Cell copied into HTML email:

line1

        <===== Cr+Lf are automatically added
line2

        <===== Cr+Lf are automatically added
line3

        <===== Cr+Lf are automatically added
line4

        <===== Cr+Lf are automatically added

从上方可以看到,将带有WordWrap的Excel单元格复制到HTML电子邮件中时,每行添加Cr + Lf。 如何仅删除添加的Cr + Lf才能具有原始格式的文本?

我已经尝试使用与Outlook中存在的“选择性粘贴”不同的所有选项,但所有选项均无法正常工作。 唯一可以正常工作的是粘贴特殊“图片增强型图元文件”,但是在我需要将Excel中存在的数据复制并粘贴到电子邮件中之后,就无法使用此选项。

您能提出其他建议吗?

已经在没有目标的情况下尝试将所有“选择性粘贴”选项都应用到Outlook中。

已复制的excel多行自动换行单元已正确复制到HTML电子邮件中,而没有在每行添加新的Cr + Lf字符。

2 个答案:

答案 0 :(得分:0)

@Samuel,我可以共享代码,但是很长。 VBA基于UferForm,在该UferForm中,客户端将其数据插入简单的oneline中,TextBox被迫使用ListBox,最后是TexBox(多行),在该处可以添加无字符数限制的描述。 之后,将数据正确复制到Excel中(因为我需要它),并且在客户选择的情况下,直接将其粘贴到HTML Outlook电子邮件中并发送。 从Excel到HTML电子邮件,我使用Ron de Bruin https://www.rondebruin.nl/win/s1/outlook/bmail2.htm中的代码使用RangetoHTML方法 您可以轻松地重现我的问题,用多行条目(Alt + Enter)创建一个简单的excel单元,然后复制并粘贴到Outlook HTML电子邮件中。 Outlook粘贴将所有回车符(Alt + Enter)都解释为双回车符,因此初始格式已消失。其他“粘贴特殊方法”没有用,因为我需要从“电子邮件”中的该单元格复制并粘贴数据。

以下是复制和粘贴多行时从Excel到HTM电子邮件的示例:

Imput data inserted into UserForm

Data copied from UserForm to Excel

Paste Special from Excel is wrongly to HTML email

答案 1 :(得分:0)

@Samuel,下面的代码用于将数据从UserForm文本框“描述”复制到“ Desc_Main” excel单元格中,然后用于生成HTML电子邮件

ThisWorkbook.Names("desc_main").RefersToRange.Cells(1, 1).Value = description.Value

Sub Mail_Sheet_Outlook_Body()
On Error GoTo CodeError

    Dim OutApp As Object
    Dim OutMail As Object
    Dim data As Date
    Dim risultato As String
    Dim rng As Range
    Dim area As Range
    Set area = ThisWorkbook.Names("Print_Area").RefersToRange

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

    Set rng = Nothing
    Set rng = area

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

    data = Now()
    data = Format(data, "dd/mm/yyyy")

    On Error Resume Next
    With OutMail
    .to = ThisWorkbook.Names("address1").RefersToRange.Cells(1, 1).Value & "; " & ThisWorkbook.Names("address2").RefersToRange.Cells(1, 1).Value
    .CC = ThisWorkbook.Names("address3").RefersToRange.Cells(1, 1).Value & "; " & ThisWorkbook.Names("address4").RefersToRange.Cells(1, 1).Value & "; " & email_address.Value


' Subject Email creation

Dim campo1 As String

    .Subject = "New ticket request - " & service_type.Value

    .HTMLBody = "<font size=""2"" face=""Tahoma"">" _
                & "Hi, please raise the following ticket:" _
                & "<p>" _
                & RangetoHTML(rng) _
                & "<p>" _
                & "Best Regards" _
                & Chr(10)

    .Display

    End With
    On Error GoTo 0

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

    Set OutMail = Nothing
    Set OutApp = Nothing


CodeError:
If Err <> 0 Then
MsgBox "Error: (" & Err.Number & ") " & Err & Error(Err) & Err.description, vbCritical
End If

End Sub

Function RangetoHTML(rng As Range)

On Error GoTo CodeError

    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

CodeError:
If Err <> 0 Then
MsgBox "Error: (" & Err.Number & ") " & Err & Error(Err) & Err.description, vbCritical
End If


End Function