如何从RangetoHTML函数中保存XLSX以用作OL附件

时间:2019-05-09 02:59:34

标签: excel vba

我目前能够成功复制保留格式的一系列单元格到电子邮件正文中,但是,我还想附加一个XLSX文件,该文件可以反映电子邮件正文中显示的内容。无法在函数和电子邮件例程之间传递文件变量。

.HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>Hello " & StaffName & ", <br />  <br /> Reach out if you have any questions/concerns." & vbNewLine & vbNewLine & RangetoHTML(rng)

.Attachments.Add ****VARIABLE FROM Function RangetoHTML****

Function RangetoHTML(rng As Range)
    Dim myFileName As String

    '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 xlPasteAllMergingConditionalFormats, , False, False
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        '.Cells(1).PasteSpecial xlPasteAllUsingSourceTheme, , False, False
        .Cells(1).Select
        ActiveSheet.Range("A:A").ClearContents
        ActiveSheet.Range("A:A").ClearFormats
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0

    'FOLLOWING THREE LINES SAVE THE FILE THAT NEEDS TO BE PASSED BACK TO OUTMAIL ROUTINE
    myFileName = Environ$("temp") & "\" & "File-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm-ss") & ".xlsx"
    ActiveSheet.Copy
    .SaveAs Filename:=myFileName, FileFormat:=51, CreateBackup:=False

我能够保存xlsx格式的文件,该文件用作生成插入到电子邮件正文中的HTML的临时文件。我很难将绝对文件位置传递到定义电子邮件附件的例程中。

1 个答案:

答案 0 :(得分:0)

更改

Function RangetoHTML(rng As Range)

Function RangetoHTML(rng As Range) As String

并在函数主体中添加:

RangetoHTML = tempWB.FullName