Rename Workbook before sending email with Macros based on Cell Values

GamerNeelie

New Member
Joined
May 21, 2022
Messages
25
Office Version
  1. 2013
Platform
  1. Windows
I Have an email script that will send a email using a button

I need to be able to rename the file based on 3 Cell values before it sends the email

VBA Code:
Sub Email()
'This example send the last saved version of the Activeworkbook
    Dim OutApp As Object
    Dim OutMail As Object

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

    On Error Resume Next
    With OutMail
        .To = Range("X4").Value
        .CC = ""
        .BCC = ""
        .Subject = "Report - " & Range("X11").Value & " - " & Range("X9").Value & " - " & Range("E6").Value
        .Body = "Please find Attached our Report for " & Range("X11").Value & vbNewLine & vbNewLine & _
                "Company: " & Range("X9").Value & vbNewLine & _
                "ID: " & Range("E6").Value

        .Attachments.Add ActiveWorkbook.FullName
        '.Send
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
    'MsgBox "Email Sent "
End Sub

I have it sending the current workbook and filename but there is 100 business that will be sending this email to a master address so I need to be able to rename the files based on 3 cell values
A1 = Company ID
A2 = Company Name
A3 = Other Text

Thanks.
 
Ok I took the " - " off at the end and now it has saved the file as the correct information

Its now getting an error on the line further down (.Attachments.Add ThisWorkbook.Path & Application.PathSeparator & filename1 & ".xlsm")

FC1.png
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The file is saving and giving it the required file name so that all working fine. Its just stopping when it tries to find the file and add it to email
 
Upvote 0
Sorry I have fixed it now. I was playing about with the way it saves the file name and didnt copy it to the 2nd "filename1" line so they wasnt equal

it is all working perfect now

thank you so much for you help :D
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,711
Members
449,118
Latest member
MichealRed

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