VBA - Customizing Excel file name using named range and attaching to email

AmeliaBedelia

New Member
Joined
Apr 8, 2018
Messages
19
I have figured out how to customize an excel file name and attach it to an email, but cannot seem to get the code to work using a named range in my workbook. I want to use a named range so the file name is automatically adjusted based on information within the workbook.

This is the code that creates the temporary file name of "FeedbackForm.xlsx":
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\FeedbackForm.xlsx"

I tried doing the following:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx"

When doing this I also altered the following lines of code to match this new name:
.Attachments.Add (ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx")
Kill ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx"


But it produces the following error: "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed" and highlights:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx"


Here is the code that produces an excel attachment called "FeedbackForm.xlsx", but how do I get it to customize the file name based off a named Range (FormName)?


Dim OutApp As Object
Dim OutMail As Object
Dim FileName As String
Dim myFile As Variant

'Copy sheet to new workbook
Sheets("Form").Select
Sheets("Form").Copy



ActiveWorkbook.SaveAs ThisWorkbook.Path & "\FeedbackForm.xlsx"

' Open Outlook and start a new mail item
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.logon
Set OutMail = OutApp.CreateItem(0)

'Build email item and send
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.body = "Please see the attached document for feedback." & _
vbCrLf & "Thank you." & vbNewLine & vbCrLf & vbNewLine & "(signature of BEA)"
.Attachments.Add (ThisWorkbook.Path & "\FeedbackForm.xlsx")
.Display
End With

'Delete temporary Excel file
ActiveWorkbook.Close SaveChanges = True
Kill ThisWorkbook.Path & "\FeedbackForm.xlsx"

'Memory cleanup
Set OutMail = Nothing
Set OutApp = Nothing
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
The back-slash does not seem to appear once I saved. There should be a back-slash in the following statements:

I tried doing the following:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx"

When doing this I also altered the following lines of code to match this new name:
.Attachments.Add (ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx")
Kill ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx"


But it produces the following error: "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed" and highlights:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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