Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Referncing Prior Month

Status
Not open for further replies.

harryhoudini66

Technical User
Jun 20, 2006
90
US
Hello,

I have a script I am using (thanks to Skip) for sending out a report daily. I now need assistance with the monthly report. The report will be sent out the first of the month and I would like the subject line to referance the prior month. Here is what I have currently. What do I need to change to have it enter the prior month automatically?

Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
objMailItem.Display
strEmailAddr = "recipient@e-mail.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Subject = "Prior Month Totals for "& Date-1
objMailItem.HTMLBody = "Attached is the prior month report."
objMailItem.Attachments.Add "\\fileserver2\PriorMonthReport.xlsx"
objMailItem.Display
Set objMailItem = nothing
Set objOutl = nothing
 
[red]month([/red]date()[red])[/red] - 1

-Geates
 
better yet, include dateAdd():

Code:
aDate = dateAdd("m", -1, date())
intMonth = month(aDate)
strMonth = monthName(intMonth)

msgbox strMonth & " is month number " & intMonth & " of the year"

using dateadd() guarantees the prior month is 12, instead of 0, if the current month is January.

-Geates
 
Thank you. How about if I want the month name written? For example:

Prior Month Totals for October

The first option looks like it may work but gives the number instead of the name.
 
Okay, it looks like this worked.


Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
objMailItem.Display
strEmailAddr = "recipient@e-mail.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Subject = "Prior Month Totals for "& MonthName(Month(Now) - 1)
objMailItem.HTMLBody = "Attached is the prior month report."
objMailItem.Attachments.Add "\\fileserver2\PriorMonthReport.xlsx"
objMailItem.Display
Set objMailItem = nothing
Set objOutl = nothing

 
Sorry, while I am at it. How do I insert the month name in the body of the message as well?
 
Man I know I am coming accross as a total with all the questions. What if I also want to include the year?

 
Found something that works, not sure if ideal so feedback welcomed:


Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
objMailItem.Display
strEmailAddr = "recipient@e-mail.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Subject = "Prior Month Totals for "& MonthName(Month(Now) - 1)& Space(1) & Year(Now())
objMailItem.HTMLBody = "Attached is the prior month report."
objMailItem.Attachments.Add "\\fileserver2\PriorMonthReport.xlsx"
objMailItem.Display
Set objMailItem = nothing
Set objOutl = nothing

 
It will work until January, and then it will fail. See Geates' second reply.

Also, when calculating the year, you don't want the current year... you want to show the year that last month fell in.

I think the code below would give what you want.
Code:
Dim strLastMonth, strLastMonthYear
strLastMonth = monthName(month(dateAdd("m", -1, Date())))
strLastMonthYear = Year((dateAdd("m", -1, Date())))

....

objMailItem.Subject = "Prior Month Totals for " & _
   strLastMonth & Space(1) & strLastMonthYear
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top