简体   繁体   English

将 Excel 范围粘贴到没有列结构的 Outlook 电子邮件正文

[英]Paste Excel range to body of Outlook email with no column structure

I'm looking to copy an Excel range to the body of an Outlook email.我希望将 Excel 范围复制到 Outlook 电子邮件的正文中。

The problem is that with the code I have, the Outlook email body preserves the column structure of the Excel range and I just want text (with bold and italic like in the Excel file).问题是,对于我拥有的代码,Outlook 电子邮件正文保留了 Excel 范围的列结构,而我只想要文本(与 Excel 文件中的粗体和斜体一样)。

I just want simple text in the Email-body of Outlook.我只想要 Outlook 的电子邮件正文中的简单文本。

Sub excelrange()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

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

With Sheets(Range("D3").Value)
    Set rng = Sheets("Gibraltar").Range("a16:d47")
End With

On Error Resume Next
With OutMail
    .To = ActiveSheet.Range("b13")
    .CC = ActiveSheet.Range("b14")
    .BCC = ""
    .Subject = ActiveSheet.Range("b11")
    .HTMLBody = RangetoHTML(rng)
    .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


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 here

只需从您的代码/函数 RangetoHTML(rng As Range) 中删除以下行

.Cells(1).PasteSpecial xlPasteFormats, , False, False

This is more some ideas than an answer, but it may help you decide what you want.这与其说是答案,不如说是一些想法,但它可能会帮助您决定自己想要什么。

If you want formatting such as bold or italic, the email body must be Html.如果您想要粗体或斜体等格式,电子邮件正文必须是 Html。 You could have a file containing something like:您可能有一个包含以下内容的文件:

<p>Dear %receiver%,<br>Good day.</p>    
<p>Please find following other request details:</p> 
<p>Name: %name%<br>I: <b>%id%</b><br>Flag: %flag%</p>
<p>Ammmmm: %xxx% %yyyyy%<br>Collection Date: <i>%colldate%</i></p> 

This is valid, if incomplete, Html.这是有效的,如果不完整,Html。 If you do not know Html:如果您不知道 Html:

  • <p> … </p> is a paragraph <p> ... </p> 是一个段落
  • <br> is a line break <br> 是换行符
  • <b> … </b> is bold <b> ... </b> 是粗体
  • <i> … </i> is italic <i> ... </i> 是斜体

Strings such as “%receiver%” and “%colldate%” would be substituted at runtime with the values from the appropriate cell of the range.诸如“%receiver%”和“%colldate%”之类的字符串将在运行时替换为范围内相应单元格中的值。 You would need another file to specify the substitution.您需要另一个文件来指定替换。 Something like:就像是:

receiver   C15   
name       A4     
id         B3     
flag       B4
colldate   B6      m/dd/yyyy

The first column is the code string (without the percent signs) that appears in the template.第一列是出现在模板中的代码字符串(没有百分号)。 The second column is the cell within the range that contains the value to replace the code.第二列是包含要替换代码的值的范围内的单元格。 If you want output numbers and dates to be formatted, you could have a third column specifying the Number format.如果您希望格式化输出数字和日期,您可以使用第三列指定数字格式。

With the above approach, your VBA would need to read the two files and then loop down the list in the second file replacing the strings with the cell values.使用上述方法,您的 VBA 需要读取这两个文件,然后向下循环第二个文件中的列表,用单元格值替换字符串。

Since you are using Excel, these two files could be worksheets.由于您使用的是 Excel,因此这两个文件可能是工作表。 The second file is already a table so maps easily to Excel.第二个文件已经是一个表格,因此很容易映射到 Excel。 The first file would need to be converted to a worksheet something like:第一个文件需要转换为工作表,例如:

Row  Column A
  1  Dear %receiver%,
     Good day.  
  2  Please find following other request details:   
  3  Name: %name%
     I: <b>%id%</b>
     Flag: %flag%
  4  Ammmmm: %xxx% %yyyyy%
     Collection Date: <i>%colldate%</i> 

I have left Bold and italic as Html tags in this worksheet.我在此工作表中将粗体和斜体保留为 Html 标记。 You could use Excel formatting but then your code would have to search for bold and italic characters within each cell and generate the necessary Html.您可以使用 Excel 格式,但是您的代码必须在每个单元格中搜索粗体和斜体字符并生成必要的 Html。 This would slow your routine and be more advanced and much trickier VBA that would otherwise be needed.这会减慢您的例行程序,并且会变得更高级和更棘手的 VBA,否则将需要这些 VBA。 I can explain what would be involved if you want to know.如果你想知道,我可以解释会涉及到什么。

You would also need to replace vbLF within the cells with "<br>" and surround the value with “<p>” and “</p>”.您还需要将单元格中的 vbLF 替换为“<br>”,并用“<p>”和“</p>”将值括起来。

With the one exception I have explained, the VBA to achieve this conversion would be quite simple.除了我解释过的一个例外,实现这种转换的 VBA 将非常简单。 I would expect any online, Excel-VBA tutorial to cover the necessary statements.我希望任何在线 Excel-VBA 教程都能涵盖必要的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM