在 Excel 中使用 VBA,如何在 Outlook 中粘贴表格然后将表格转换为文本?
Posted
技术标签:
【中文标题】在 Excel 中使用 VBA,如何在 Outlook 中粘贴表格然后将表格转换为文本?【英文标题】:Using VBA in Excel, how to I paste a Table in Outlook then convert the table to text? 【发布时间】:2019-04-06 19:28:01 【问题描述】:我目前正在尝试编写一个 VBA 函数,该函数将在 Excel 中复制一个范围并将其粘贴到 Outlook 电子邮件中。我创建的东西可以成功地将表格和格式复制到 Outlook 中,但问题是表格很难在手机上阅读。我想简单地使用Outlook“ConvertToText”将表格更改为文本格式,这样更容易阅读。不过,我想不出办法做到这一点。我的代码如下,使用一个函数和一个子:
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim SDest As String
Dim i As Integer
Set rng = Nothing
On Error Resume Next
'You can also use a fixed range if you want
Set rng = Sheets("Email").Range("A2:Q400").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
SDest = ""
For i = 1 To WorksheetFunction.CountA(Sheets("Email List").Columns(1))
If SDest = "" Then
SDest = Sheets("Email List").Cells(i, 1).Value
Else
SDest = SDest & ";" & Sheets("Email List").Cells(i, 1).Value
End If
Next i
.To = SDest
.CC = ""
.BCC = ""
.Subject = "Frontside Update"
.htmlBody = RangetoHTML(rng)
.Display 'or use .Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
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
任何帮助将不胜感激。我不确定我是否做错了并且需要重新开始?最初我希望 HTML 表格能很好地工作,但后来意识到它在手机上看起来很糟糕(许多收件人都使用它)并且不知道最好的解决方案。
【问题讨论】:
【参考方案1】:如果你想要纯文本,那么可以试试这个:将范围复制到一个数组。遍历数组并组装一个字符串,在单元格值之间添加制表符并在行之间返回。 (您也可以遍历工作表上的单元格,但宏在数组中运行得很快。)
Dim arrCells(), r, c, strBody
arrCells = Selection
For r = LBound(arrCells, 1) To UBound(arrCells, 1)
For c = LBound(arrCells, 2) To UBound(arrCells, 2)
strBody = strBody & vbTab & arrCells(r, c).Value
Next c
strBody = strBody & vbCrLf
Next r
【讨论】:
以上是关于在 Excel 中使用 VBA,如何在 Outlook 中粘贴表格然后将表格转换为文本?的主要内容,如果未能解决你的问题,请参考以下文章