Save excel in specific folder after sending mail as attachement using VBA

ebineg

New Member
Joined
Feb 24, 2016
Messages
39
Hi Guys,

I am currently using a macro which will send the working sheet as attachment in mail, but i also wanted the attachment to get saved in the specific folder in my local.Below is my current code , it will be really helpful if i can have the code for saving the attachment in local folder.

Local folder path : C:\Users\xyz\Desktop\macro

Code:
Sub SendMail()
   'Turn off screen updating
   Application.ScreenUpdating = False
  
   'Activate the correct sheet in the workbook
   ActiveWorkbook.Sheets("Catalog Integration Formatter").Activate
  
   'EMAIL SHEET AS ATTACHMENT
   Dim LWorkbook As Workbook
   Dim LFileName As String
   Dim Provider As String
   Dim Delim As String
   Dim file_date As String
   Dim subject_date As String
   Dim Build As String
   Dim Marketplace As String
   Dim emailString As String
   Provider = Range("D1").Value
   Delim = " - "
   file_date = Format(Now, "MMM,d,yyyy")
   subject_date = Now()
   Build = Range("F3").Value
   Marketplace = Range("F4").Value
   emailString = Range("L2").Value
   
   ' Add current time to Date cell
   Range("F5") = Format(subject_date, "mm/dd/yyyy h:mm AM/PM")
   
    'Align the columns pasted in from the CSV file
    Range("E13:E32,E35:E54,E57:E76,E79:E98").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    Range("F13:F32,F35:F54,F57:F76,F79:F98,H13:H32,H35:H54,H57:H76,H79:H98").Select
    With Selection
        .VerticalAlignment = xlCenter
        .WrapText = True
    End With
   Range("D1").Select
   
   'Copy the active worksheet and save to a temporary workbook
   ActiveSheet.Copy
   Set LWorkbook = ActiveWorkbook


   'Create a temporary file in your current directory that uses the name of the sheet as the filename
   LFileName = Provider & Delim & file_date
   On Error Resume Next
   'Delete the file if it already exists
   Kill LFileName
   On Error GoTo 0
   'Save temporary file
   LWorkbook.SaveAs Filename:=LFileName, FileFormat:=52


   ' Select the table (or range of cells) on the active worksheet.
   ActiveSheet.Columns("D:J").Select
   
   ' Show the envelope on the ActiveWorkbook.
   ActiveWorkbook.EnvelopeVisible = False
   
   ' Set email destination, subject line and send
   With ActiveSheet.MailEnvelope
      .Item.Subject = Provider & Delim & Marketplace & Delim & Build & Delim & subject_date
      .Item.To = emailString
      .Item.Attachments.Add ActiveWorkbook.FullName
      .Item.Send
   End With
      
   'Delete the temporary file and close temporary Workbook
   LWorkbook.ChangeFileAccess Mode:=xlReadOnly
   Kill LWorkbook.FullName
   LWorkbook.Close SaveChanges:=False


   'Turn back on screen updating
   Application.ScreenUpdating = True
   
End Sub
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi so from what I can see here it already gets saved but then deleted. BUT! I'll add in some extra code to build your filepath if it does not exist, so if you change:

Code:
LWorkbook.SaveAs Filename:=LFileName, FileFormat:=52
To:

Code:
<code> Dim elm As Variant
Dim strCheckPath As String
Dim strPath As String

    strPath = "E:\Users\xyz\Desktop\macro"
    strCheckPath = ""
    For Each elm In Split(strPath, "\")
        strCheckPath = strCheckPath & elm & "\"
        If Len(Dir(strCheckPath, vbDirectory)) = 0 Then MkDir strCheckPath
    Next elm
    LWorkBook.SaveAs Filename:=strPath & "\" & LFileName, FileFormat:=52</code>

And also remove these lines:

Code:
'Delete the temporary file and close temporary Workbook
LWorkbook.ChangeFileAccess Mode:=xlReadOnly
Kill LWorkbook.FullName

Then it should save it to your desired location.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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