在Excel中使用VBA,如何在Outlook中粘贴表格,然后将表格转换为文本?

时间:2018-11-02 14:31:07

标签: excel vba outlook

我目前正在尝试编写一个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

a

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 个答案:

答案 0 :(得分:0)

如果要纯文本,则可以尝试以下操作:将范围复制到数组。遍历数组并组装一个字符串,在单元格值之间添加制表符,并在行之间返回。 (您也可以在工作表中的单元格之间循环,但是宏可以通过数组快速运行。)

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