简体   繁体   English

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

[英]VBA macro to copy and paste range into email

Hi I have a macro that creates a PDF then creates an email and sends the pdf. 嗨,我有一个宏,可以创建PDF,然后创建电子邮件并发送pdf。 I now need to copy a range of cells and paste them into the body of the email. 现在,我需要复制一系列单元格并将其粘贴到电子邮件正文中。 I have seen examples of copy and paste macros but do not know how to combine that code into the create and send macro. 我已经看到了复制和粘贴宏的示例,但不知道如何将代码组合到创建和发送宏中。

any help appreciated 任何帮助表示赞赏

Thanks 谢谢

Thanks - Here is the current code 谢谢-这是当前代码

This was from rondebruin.nl (with a little bit of editing.) 这是来自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

What's the nature of the ranges you're copy/pasting? 您要复制/粘贴的范围的性质是什么? If content is something short, consider setting them to variables. 如果内容太短,请考虑将其设置为变量。

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

then change your code to 然后将您的代码更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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