VBA宏将范围复制并粘贴到电子邮件

时间:2015-06-16 06:18:45

标签: excel-vba vba excel

您好我有一个创建PDF的宏然后创建一个电子邮件并发送pdf。我现在需要复制一系列单元格并将它们粘贴到电子邮件的正文中。我已经看到了复制和粘贴宏的示例,但不知道如何将该代码组合到创建和发送宏中。

任何帮助表示赞赏

由于

谢谢 - 这是当前的代码

这是来自rondebruin.nl(稍加编辑。)

Sub new_save_as()


Dim OlApp As Object
Dim NewMail As Object
Dim TempFilePath As String
Dim TempFileName As String
Dim FileFullPath As String

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
TempFilePath = Environ$("temp") & "\"

TempFileName = "Rewards desk report" & " " & Format(Now - 1, "dd-mmm-yy") & ".pdf"

FileFullPath = TempFilePath & TempFileName

On Error GoTo err
With ActiveSheet
        .ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=FileFullPath, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
End With

Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)

On Error Resume Next
With NewMail
    .To = "address here"
    .CC = "address here"
    .BCC = ""
    .Subject = "Rewards desk daily report"
    .Body = "The daily report is attached"
    .Attachments.Add FileFullPath
    .Display  
End With
On Error GoTo 0

Kill FileFullPath

Set NewMail = Nothing
Set OlApp = Nothing

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
MsgBox ("Email has been Sent Successfully")
Exit Sub
err:
    MsgBox err.Description

End Sub

1 个答案:

答案 0 :(得分:0)

您复制/粘贴的范围的性质是什么?如果内容很短,请考虑将它们设置为变量。

msg1 = Range("A1").Value
msg2 = Range("A2").Value
msg3 = Range("A3").Value 

然后将代码更改为

.Body = "The daily report is attached" & vbCrLf & msg1 & " " & msg2 & " " & msg3