使用VBA在电子邮件中附加Excel文件

时间:2018-11-29 12:48:24

标签: excel vba excel-vba excel-formula

我正在尝试解决VBA的问题,以便在用户发送之前将Excel中的文件直接附加到带有.Display的电子邮件中。该文件应保存,创建一个新的临时文件,然后将该临时文件直接复制并粘贴到电子邮件正文中,同时将实际的原始文档附加到电子邮件中以供参考。完成此操作后,它应该杀死临时文件而不保存它。

我遇到的问题是它将创建新的临时工作簿,但不会将信息复制并粘贴到电子邮件中或将文档附加到电子邮件中。我的代码如下,带有股票电子邮件地址。任何帮助表示赞赏。错误消息似乎总是出现在.Attachments.Add (ActiveDocument.FullName)

Sub SendEmailOutlook()
    ActiveWorkbook.Save

    'Send an email. basically just to standardize and error-proof the process
    'RangetoHTML function (below this macro) allows a range of cells to be pasted into the email body    
    Dim strbody As String    
    Dim rng As Range    
    Dim OutApp As Object    
    Dim OutMail As Object    
    Dim Message As String    
    Dim subject As String    
    Dim UpdateTime As String        

    'this line is extra for HTML formatting but it makes the text easier to read    
    strbody = "<P STYLE='font-family:Calibri;font-size:12pt'>"

    subject = "2018 Safety Walk Form for " & Sheets("2018 Safety Walk").Range("H5") & " " & Sheets("2018 Safety Walk").Range("K5")

    Message = "Team <br><br>Please see the attached form for, " & Sheets("2018 Safety Walk").Range("K5")

    'Set last row based on input data
    '    Dim lastRow As String    
    '    lastRow = Sheets("Email").UsedRange.Rows.Count
    'Set range for email body    
    'The column is on the right, the row on the left. Change to .Range("A1:H30") if you want it to be static

    Set rng = Sheets("2018 Safety Walk").Range("B5:K43")

    'Create email
    With Application    
        .EnableEvents = False    
        .ScreenUpdating = False    
    End With

    Set OutApp = CreateObject("Outlook.Application")    
    Set OutMail = OutApp.CreateItem(0)

    With OutMail    
        .To = "123@123.com"    
        .BCC = ""    
        .subject = subject    
        .HTMLBody = strbody & Message & RangetoHTML(rng) & "<br>"    
        .Attachments.Add (ActiveDocument.FullName)        
        .Display    'you can use .Send to have the macro send the email without needing to confirm it    
    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         

    '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

1 个答案:

答案 0 :(得分:1)

根据OP的评论:

  

运行宏时,收到的错误消息是“运行时错误   '-2147024894(80070002)':找不到此文件。验证路径并   文件名正确。”

这是因为在您的RangeToHTML函数中,您创建的是两个临时工作簿,但只能关闭其中一个。因此,RangeToHTML返回后,您的ActiveWorkbook是指剩余的临时工作簿,而不是原始的工作簿。由于尚未保存,因此它的.FullName属性还没有值,因此出现“找不到此文件”错误。

RangeToHTML中,删除以下其中一个要复制/粘贴的部分,您的问题将得到解决:

'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         

'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

SendEmailOutlook过程中,您也应该根本不使用ActiveWorkbook,因为如您所见,如果不小心,这会使您绊倒。最好直接设置和使用对工作簿的引用。

所以,像这样:

Dim wb as Workbook
Set wb = Workbooks("Your Workbook Name")

或:

Dim wb as Workbook
Set wb = ActiveWorkbook

SendEmailOutlook的开头,然后在过程中其他位置当前使用wb的任何地方使用ActiveWorkbook