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