简体   繁体   中英

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. 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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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