+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    VBA to send e-mail

    Hi,

    I have got the following code that automatically copies a range of cells within the sheet i am on and then creates a new excel workbook then sends it to an e-mail address specified.

    Is there anyway this code can be changed so that it doesnt copy/paste the info from A1:K50 but copies the info from the cells that you select manually e.g. if i select C6:F10 then click the macro thats what i want to be copied but then later i select A1:C20 thats what i want to be copied.

    HTML Code:
    Sub Mail_Range()
        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 Long
    
        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 = "Selection 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
            FileExtStr = ".xlsx": FileFormatNum = 51
        End If
    
        With Dest
            .SaveAs TempFilePath & TempFileName & FileExtStr, _
                    FileFormat:=FileFormatNum
            On Error Resume Next
            .SendMail "E-Mail Address Here", _
                      "This is the Subject line"
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
    
        Kill TempFilePath & TempFileName & FileExtStr
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Regards,
    Jamie
    Last edited by tmtjamie; 02-24-2010 at 07:37 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,493

    Re: VBA to send e-mail

    Hello Jamie,

    Only one line in the macro needs to changed. It is marked below.
    Code:
    Sub Mail_Range()
        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 Long
    
        Set Source = Nothing
        On Error Resume Next
        Set Source = Selection   '<<<<< Change this line
        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 = "Selection 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
            FileExtStr = ".xlsx": FileFormatNum = 51
        End If
    
        With Dest
            .SaveAs TempFilePath & TempFileName & FileExtStr, _
                    FileFormat:=FileFormatNum
            On Error Resume Next
            .SendMail "E-Mail Address Here", _
                      "This is the Subject line"
            On Error GoTo 0
            .Close SaveChanges:=False
        End With
    
        Kill TempFilePath & TempFileName & FileExtStr
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA to send e-mail

    Thank you very much Leith... i will try this in the morning.

  4. #4
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA to send e-mail

    This works great thank you for your help...

    in addition to this is it possible to change the first code so that it does copy the range of cells i have specified (just the way the code is now) but then also changes the colour of specific cells based on my selection at the point of pressing the button.

    e.g.
    i have got the code to copy this range A1:K50 which is fine but i also want it to change the colour of the cells (in the sheet that is sent via e-mail) i had selected when i clicked the button to yellow, so just like the code you posted but this time instead of only copying those cells it acutally changes the colours of those cells?

    Regards,
    Jamie

  5. #5
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA to send e-mail

    I have tried to do this by inserting this code (probably all wrong )

    Code:
    source = selection
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("A1").Select
    I got this code from recording myself chaging the colour of some cells

  6. #6
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA to send e-mail

    I know you are busy but anyone know?

  7. #7
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA to send e-mail

    Anyone ?? :-)

  8. #8
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    768

    Re: VBA to send e-mail

    I don't think you have asked a question.

    You state that the code change identified by Leith works; you then say you have changed the code using the code you recorded. You don't say whether this worked or didn't work. If it didn't :you don't say what went wrong. And you haven't posted the revised code in which you have pasted the colour change.

    Or am I missing something?


    Hope this was useful or entertaining.

  9. #9
    Registered User
    Join Date
    01-14-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: VBA to send e-mail

    Sorry if there is a lack of info...... the code that Leith Ross posted did work and i am using that on some sheets... i then said the following which does state it works and then follows with the question, i also say based on the first post/code so there is no revised code

    Quote Originally Posted by tmtjamie View Post
    This works great thank you for your help...

    in addition to this is it possible to change the first code so that it does copy the range of cells i have specified (just the way the code is now) but then also changes the colour of specific cells based on my selection at the point of pressing the button.

    e.g.
    i have got the code to copy this range A1:K50 which is fine but i also want it to change the colour of the cells (in the sheet that is sent via e-mail) i had selected when i clicked the button to yellow, so just like the code you posted but this time instead of only copying those cells it acutally changes the colours of those cells?

    Regards,
    Jamie
    The quoted post is a slightly different request but still based on the same idea/code so i didnt want to start a new thread.

  10. #10
    Registered User
    Join Date
    01-26-2010
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: VBA to send e-mail

    Sorry... JDobbsy1987 is my mate, i didn't realise he was logged in on my PC.

    Regards,
    Jamie

  11. #11
    Registered User
    Join Date
    11-13-2009
    Location
    Lx
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA to send e-mail

    hi,

    if I have powers to send e-mail via another e-mail using "on behalf function" how can I adjust the macro?

    regards

Thread Information

Users Browsing this Thread

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

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