Copy the sheet to a new file and send it by email By VBA

Omer_K

Board Regular
Joined
Apr 9, 2017
Messages
124
Office Version
  1. 365
Hey all :)

I would like to know how by VBA code I can copy the Active sheet to a new Excel file - the file name will be a combination of cells A2 and A3 and A4 in another sheet called for example TEST
And attaching the new file as a new email ..

Hope for your help friends :)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this macro. Change the saveas path (in red) and the sheet name (in blue) to suit your needs.
Rich (BB code):
Sub CreateEmail()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, WS As Worksheet, fName As String
    With Sheets("TEST")
        fName = .Range("A2").Value & " " & .Range("A3").Value & " " & Range("A4").Value
    End With
    ActiveSheet.Copy
    ActiveWorkbook.SaveAs Filename:="C:\Test\" & fName & ".xlsx"
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = ""
        .Subject = ""
        .HTMLBody = ""
        .attachments.Add ActiveWorkbook.FullName
        .Display
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this macro. Change the saveas path (in red) and the sheet name (in blue) to suit your needs.
Rich (BB code):
Sub CreateEmail()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, WS As Worksheet, fName As String
    With Sheets("TEST")
        fName = .Range("A2").Value & " " & .Range("A3").Value & " " & Range("A4").Value
    End With
    ActiveSheet.Copy
    ActiveWorkbook.SaveAs Filename:="C:\Test\" & fName & ".xlsx"
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = ""
        .Subject = ""
        .HTMLBody = ""
        .attachments.Add ActiveWorkbook.FullName
        .Display
    End With
    Application.ScreenUpdating = True
End Sub

Hey,
Thank you :)
I copy and run this code but I get this error:
subscript out of range

Any Ideas?
 
Upvote 0
When you click "Debug", which line of code is highlighted?
 
Upvote 0
I made a mistake in locating the folder - it works great !! ??
Thank you!! :)
small question..
Is it possible to send the file without having to save the file to my computer?
 
Upvote 0
This line of code attaches the file using its full name.
VBA Code:
.attachments.Add ActiveWorkbook.FullName
"FullName" represents the full path to where the file is saved. This line of code will not work unless the filed is saved before it is attached to the email.
 
Upvote 0
I have this in my Personal Macro Workbook for that task and includes deleting the Temporary file used to send the Sheet. It's not a task I haven't had need of for several years.
! Do note that the proper credit goes to Ron Debruin and the webpage refenced in the code.
The section where it converts formulas to values is necessary if there may be external references which would be invalid on the receivers computer/network.
VBA Code:
Sub Mail_ActiveSheet()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim DestWB As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set DestWB = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With DestWB
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56: FileExtStr = ".xls": FileFormatNum = 56
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
            End Select
        End If
    End With

    '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With DestWB
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = "This is the Subject line"
            .Body = "Hi there"
            .Attachments.Add DestWB.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Display or .Save
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top