Outlook VBA - 根据日期

时间:2016-06-24 02:50:54

标签: vba outlook-vba

我正在尝试创建一个代码,该代码将根据日期替换占位符文本。 这些电子邮件必须与每个月(第7天,第14天,第21天和第28天)在特定日期生成的报告相关联。我有代码来替换占位符文本,我只是无法弄清楚如何确定将要使用的日期(例如;请提供#DATE#的报告)

如果当月的当天是在该月的第7天或之后但在14日之前,那么我希望将日期更改为当月的第7天。 (例如;请提供2016年6月7日的报告)。

如果当月的当天是在该月的第28天或之后但在下个月的第7天之前,那么我希望将日期更改为该月的第28天。 (例如;请提供2016年6月28日的报告)等。

我一直在尝试使用if语句并将当前日期日放入字符串中,但这并不起作用。

Date1 = Format(Date, "dd")

If Date1 >= 7 And Date1 <= 13 Then
Date2 = "7th"
Else

If Date1 >= 14 And Date1 <= 20 Then
Date2 = "14th"
Else

If Date1 >= 21 And Date1 <= 27 Then
Date2 = "21st"
Else
End If

If Date1 >= 28 And Date1 <= 31 Then
Date2 = "28th"
Else

If Date1 >= 1 And Date1 <= 6 Then
Date2 = "28th"
Else

End If
End If
End If
End If

obj.Subject = Replace(obj.Subject, "#DATE#", Date2)
obj.HTMLBody = Replace(obj.HTMLBody, "#DATE#", Date2)

有人能提供任何帮助吗?

1 个答案:

答案 0 :(得分:0)

Sub test()

Date1 = Format(Date, "dd")
curMonth = Format(Now, "mmmm yyyy")

Select Case Date1
    Case Is >= 28
        Date2 = "28th " & curMonth
    Case Is >= 21
        Date2 = "21st " & curMonth
    Case Is >= 14
        Date2 = "14th " & curMonth
    Case Is >= 7
        Date2 = "7th " & curMonth
    Case Else
        Date2 = "28th " & Format(Now - 7, "mmmm yyyy")
End Select

End Sub