简体   繁体   中英

Using VBA in Excel, how to I paste a Table in Outlook then convert the table to text?

I'm currently trying to write a VBA function that will copy a range in Excel and paste it into an Outlook email. I have something created that can successfully copy the table and formatting into Outlook, but the problem is the tables are very hard to read on a phone. I'd like to simply use the outlook "ConvertToText" to change the table into text formatting so it'll be easier to read. I can't figure out a way to do that, though. My code is as follows, using a function and a sub:

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

Any help would be greatly appreciated. I'm not sure if I've gone about it wrong and need to start over? Originally I was hoping an HTML table would work well, but later realized it looks terrible on phones (which many of the recipients use) and don't know the best solution.

If you want plain text, then maybe try this: Copy the range to an array. Loop through the array and assemble a string, adding tabs between cell values and returns between rows. (You could also loop through cells on the worksheet, but macros run fast with arrays.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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