1
votes

I'm using VBA for outlook, my code is as follows:

 Function RangetoHTML(rn As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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
    rn.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

The error is at .Cells(1).PasteSpecial xlPasteValues, , False, False within the RangetoHTML function.

I've used this open source function before on vba on excel before, but it seems to not work in outlook. My function opens excel from outlook and copy and pastes a few things. My intention is to send automated emails copy and pasting from a region in excel.

Does anyone have any input on this?

Thanks!

1
That is Excel vba NOT Outlook vba0m3r

1 Answers

0
votes

Outlook does not keep the enumerators of Excel (e.g. xlPasteValues), but VBA may work with their number values. Thus, try something like this:

Option Explicit

Sub TestMe()

    Dim tempWB As Workbook
    Set tempWB = Workbooks.Add(1)

    Dim rn  As Range
    Set rn = Worksheets(1).Range("A1:B10")
    rn.Copy

    With tempWB.Worksheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=-4163
    End With

End Sub 

There are two ways to see what xlPasteValues is equal to, as far as it is an enumerator, used in Excel and Outlook does not have it:

  • Option 1 - write ?xlPasteValues in the immediate window and press Enter.

  • Option 2 - Write xlPasteValues anywhere within the VBA editor, select it and press Shift + F2. Then read in the helper something like this:

enter image description here