zombiemaster
Board Regular
- Joined
- Oct 27, 2009
- Messages
- 245
I have a report that I'm developing and want to email it automatically when it's ready. I know how to do most of it, but I'm having trouble with the Subject line within the email. I want the subject line to be the file name. Below is the code that I have right now, that works fine, that includes a "hard coded" subject line of "This Month's Report" but I want it to show the file name of "FREEZE CODES - MARCH 2019".
A bit earlier in the code, I have already declared the filename, and thought I might be able to use it here, but it didn't work.
I tried changing the code to show this, but it didn't work and the Subject line in the email just ended up empty:
Thanks for any tips - it's probably just something simple, as usual...
~ZM~
Code:
' Declare and establish the Object variables for Outlook:
Dim objOutlook As Object
Dim objNameSpace As Object
Dim objInbox As Object
Dim objMailItem As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objInbox = objNameSpace.Folders(1)
Set objMailItem = objOutlook.CreateItem(0)
' An email will generate with the file as an attachment
With objMailItem
.To = "zombiemaster@noname.com"
.CC = ""
.Subject = "This month's report"
.Body = _
"Attached is the latest Freeze Code report." & Chr(10) & Chr(10) & _
"If you have any questions, let me know." & Chr(10) & Chr(10)
.Attachments.Add ActiveWorkbook.FullName
.Display
End With
' Release object variables from system memory
Set objOutlook = Nothing
Set objNameSpace = Nothing
Set objInbox = Nothing
Set objMailItem = Nothing
A bit earlier in the code, I have already declared the filename, and thought I might be able to use it here, but it didn't work.
Code:
' Saves the file with the correct month and year in the file name:
Dim Path As String
Dim filename As String
Path = "\\Server\2019 testing\"
filename = Range("F1")
ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsx"
I tried changing the code to show this, but it didn't work and the Subject line in the email just ended up empty:
Code:
.Subject = filename
Thanks for any tips - it's probably just something simple, as usual...
~ZM~