+ Reply to Thread
Results 1 to 6 of 6

Vba code copy range and paste special in email body

Hybrid View

  1. #1
    Registered User
    Join Date
    12-04-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    10

    Vba code copy range and paste special in email body

    Can anyone provide or help me with the Vba code that will allow one to select then copy a specific range (2007 excel) and paste special as a windows enhanced metafile into the email body in Outlook 2007? I'm would consider myself at the novice stage of writing Vba.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Vba code copy range and paste special in email body

    Look at this site and determine if one of the solutions here will work for you

    http://www.rondebruin.nl/win/s1/outlook/mail.htm
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    07-10-2013
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2007
    Posts
    40

    Re: Vba code copy range and paste special in email body

    Try this,

    Sub Mail_Range()
    'Working in 2000-2010
        Dim Source As Range
        Dim Dest As Workbook
        Dim wb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim FileFormatNum As Integer
        Dim I As Integer
    
        Set Source = Nothing
        On Error Resume Next
        Set Source = Range("A1:K50").SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    
        If Source Is Nothing Then
            MsgBox "The source is not a range or the sheet is protected, " & _
                   "please correct and try again.", vbOKOnly
            Exit Sub
        End If
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set wb = ActiveWorkbook
        Set Dest = Workbooks.Add(xlWBATWorksheet)
    
        Source.Copy
        With Dest.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial Paste:=xlPasteValues
            .Cells(1).PasteSpecial Paste:=xlPasteFormats
            .Cells(1).Select
            Application.CutCopyMode = False
        End With
    
        TempFilePath = Environ$("temp") & "\"
        TempFileName = "Range of " & wb.Name & " " _
                     & Format(Now, "dd-mmm-yy h-mm-ss")
    
        If Val(Application.Version) < 12 Then
            'You use Excel 2000-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2010
            FileExtStr = ".xlsx": FileFormatNum = 51
        End If
    
        With Dest
            .SaveAs TempFilePath & TempFileName & FileExtStr, _
                    FileFormat:=FileFormatNum
            On Error Resume Next
            For I = 1 To 3
                .SendMail "ron@debruin.nl", _
                          "This is the Subject line"
                If Err.Number = 0 Then Exit For
            Next I
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
    
        'Delete the file you have send
        Kill TempFilePath & TempFileName & FileExtStr
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    07-24-2012
    Location
    Mumbai
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Vba code copy range and paste special in email body

    Did not work for me. I've have a range into excel 2010 sheet say A1:G10. I want to copy this range into outlook 2010 email body with some other texts as well. e.g. Body "This is the data for your team." <enter> Then the excel range data should show <enter> then " Please process timely."

    Please help

  5. #5
    Registered User
    Join Date
    07-24-2012
    Location
    Mumbai
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Vba code copy range and paste special in email body

    SOLUTION:

    Sub Embedd_Image()
    
    Dim obj As Outlook.Application
    Dim objmail As MailItem
    
    
        Set obj = CreateObject("Outlook.Application")
        Set objmail = obj.createitem(olmailitem)
    
            With objmail
            .To = "mac.vba@gmail.com"
            .CC = "mac.vba@gmail.com"
            .Subject = "This is a sample picture embedded"
            
            .HTMLBody = "<html><p>This picture can be seen here_START.</p>" & _
                "<img src=C:\Users\mac\Pictures\apple.jpg' height=300 width=280>" & _
                "<html><p>This picture can be seen here_START.</p>" 'Height & Width can be resized
            
            .Save
            .Display
            '.Send
            End With
    
        Set obj = Nothing
        Set objmail = Nothing
    
    End Sub
    Last edited by alansidman; 09-30-2013 at 12:47 PM. Reason: Added code tags

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Vba code copy range and paste special in email body

    Mac.vba:
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added the code tags for you this time. Please read our rules and observe them in the future.)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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