+ Reply to Thread
Results 1 to 9 of 9

Copy attachment contents into body of email !

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2015
    Location
    England
    MS-Off Ver
    Microsoft Office 365
    Posts
    92

    Copy attachment contents into body of email !

    Hi all.

    I have the following code that is being used to send an attachment via email.

    IS there something I can add to the code that will allow the contents of a sheet to be copied and pasted into the body of the email.

    Any help would be much appreciated

    
    Option Explicit
    
    
    Sub Button1_Click()
    
        Dim lErrorNo    As Long
        Dim OutMail     As Object
        Dim myEmail     As String
        Dim OutApp      As Object
        Dim xNmae       As Object
    
        If ThisWorkbook.Saved = True Then
    
              Set OutApp = CreateObject("Outlook.Application")
              Set OutMail = OutApp.CreateItem(0)
              Set xNmae = OutApp.GetNamespace("MAPI")
    
              myEmail = xNmae.Session.CurrentUser.AddressEntry. _
                              GetExchangeUser.PrimarySmtpAddress
    
              With OutMail
    
                  .To = "XXXX;XXXXX"
                  .CC = myEmail
                  .BCC = ""
                  .Subject = "Ad Hoc Report Request"
                  .Body = "Hi. Please find attached my ad hoc request form."
                  .Attachments.Add ActiveWorkbook.FullName
    
                  On Error Resume Next
                      .Send
    '                 Record the number of any error which may have occurred
                      lErrorNo = Err.Number
                  On Error GoTo 0
    
    '             Display a "Success" or "Failure" message as appropriate
                  If lErrorNo = 0 Then
                        MsgBox "Your request has succesfully been sent by email.You may now close the workbook", vbInformation
                  Else: MsgBox "An error occurred - the email was NOT sent", vbCritical
                  End If
    
              End With
    
        Else: MsgBox "Please save the workbook first..", vbInformation
    
        End If
    
        Set OutMail = Nothing
        Set OutApp = Nothing
        Set xNmae = Nothing
    
    End Sub
    Last edited by V.Cell; 06-29-2016 at 05:40 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,037

    Re: Copy attachment contents into body of email !

    How about this:

                    Dim rng As Range
                    
                    Set rng = Sheets("Sheet1").Range("A41:A62")
                    '  .Body = "Hi. Please find attached my ad hoc request form." 'If needed
                    For Each r In rng
                     .Body = .Body & r
                    Next r
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    01-22-2015
    Location
    England
    MS-Off Ver
    Microsoft Office 365
    Posts
    92

    Re: Copy attachment contents into body of email !

    Quote Originally Posted by zbor View Post
    How about this:

                    Dim rng As Range
                    
                    Set rng = Sheets("Sheet1").Range("A41:A62")
                    '  .Body = "Hi. Please find attached my ad hoc request form." 'If needed
                    For Each r In rng
                     .Body = .Body & r
                    Next r
    Hi. Thanks,

    Not too sure where exactly I should place this in my code?

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,037

    Re: Copy attachment contents into body of email !

    Instead of

    .Body = "Hi. Please find attached my ad hoc request form."
    You wanted to put some range values in e-mail body?

    So you also need to edit this range and set what you trying to insert:

    Set rng = Sheets("Sheet1").Range("A41:A62")

  5. #5
    Registered User
    Join Date
    01-22-2015
    Location
    England
    MS-Off Ver
    Microsoft Office 365
    Posts
    92

    Re: Copy attachment contents into body of email !

    Quote Originally Posted by zbor View Post
    Instead of

    .Body = "Hi. Please find attached my ad hoc request form."
    You wanted to put some range values in e-mail body?

    So you also need to edit this range and set what you trying to insert:

    Set rng = Sheets("Sheet1").Range("A41:A62")
    Hi. Yes I want to include some range from one of my sheets in the body of the email in addition to the "Hi please find attached my ad hoc request form"

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,037

    Re: Copy attachment contents into body of email !

    So does it work?

  7. #7
    Registered User
    Join Date
    01-22-2015
    Location
    England
    MS-Off Ver
    Microsoft Office 365
    Posts
    92

    Re: Copy attachment contents into body of email !

    Hi.

    No it didn't work. I tried a couple of times

    Would you be able to please include it in my original VBA? It may be something incorrect I am doing

  8. #8
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,037

    Re: Copy attachment contents into body of email !

    Here, try this:

    Option Explicit
    
    
    Sub Button1_Click()
    
        Dim lErrorNo    As Long
        Dim OutMail     As Object
        Dim myEmail     As String
        Dim OutApp      As Object
        Dim xNmae       As Object
        Dim rng As Range, R As Variant
    
        If ThisWorkbook.Saved = True Then
    
              Set OutApp = CreateObject("Outlook.Application")
              Set OutMail = OutApp.CreateItem(0)
              Set xNmae = OutApp.GetNamespace("MAPI")
    
              myEmail = xNmae.Session.CurrentUser.AddressEntry. _
                              GetExchangeUser.PrimarySmtpAddress
    
              With OutMail
    
                  .To = "XXXX;XXXXX"
                  .CC = myEmail
                  .BCC = ""
                  .Subject = "Ad Hoc Report Request"
    
                   Set rng = Sheets("Sheet1").Range("A41:A62")
                      .Body = "Hi. Please find attached my ad hoc request form." 'If needed
                    For Each R In rng
                     .Body = .Body & R
                   Next R
    
                  .Attachments.Add ActiveWorkbook.FullName
    
                  On Error Resume Next
                      .Send
    '                 Record the number of any error which may have occurred
                      lErrorNo = Err.Number
                  On Error GoTo 0
    
    '             Display a "Success" or "Failure" message as appropriate
                  If lErrorNo = 0 Then
                        MsgBox "Your request has succesfully been sent by email.You may now close the workbook", vbInformation
                  Else: MsgBox "An error occurred - the email was NOT sent", vbCritical
                  End If
    
              End With
    
        Else: MsgBox "Please save the workbook first..", vbInformation
    
        End If
    
        Set OutMail = Nothing
        Set OutApp = Nothing
        Set xNmae = Nothing
    
    End Sub

  9. #9
    Registered User
    Join Date
    01-22-2015
    Location
    England
    MS-Off Ver
    Microsoft Office 365
    Posts
    92

    Re: Copy attachment contents into body of email !

    Perfect, thank you very much

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy email contents to new email whilst saving attachment.
    By X82 in forum Outlook Programming / VBA / Macros
    Replies: 25
    Last Post: 08-18-2015, 03:16 PM
  2. Email each person in excel, including a body of text and attachment
    By memanja in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2014, 10:41 AM
  3. Emailing PDF, another attachment, and with range in email body
    By supcussy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2012, 10:03 PM
  4. email excel spreadsheet as body of message, not as attachment
    By Email Spreadsheet as Body of Email Msg. in forum Excel General
    Replies: 6
    Last Post: 06-18-2012, 01:01 PM
  5. Modifying VB Code/Macro to put selection into body of email instead of attachment.
    By excelstacy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-04-2011, 05:34 PM
  6. Automatic email of excel contents as email body
    By naveenn08 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2009, 08:26 AM
  7. Email excel as body rather than attachment
    By jhahes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2006, 04:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1