如何识别通配符日期

时间:2014-06-27 09:41:52

标签: vba date outlook

当来自具有特定主题的特定发件人的电子邮件进入时,我的VBA代码将使用附件执行操作。主题行的一个例子是:

Profit and Loss Summary 06-20-2014

以下是我从http://www.jpsoftwaretech.com/outlook-vba/automate-outlook/

改编的代码
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
If TypeOf Item Is Outlook.MailItem Then
  Dim Msg As Outlook.MailItem
  Dim TwoDays As Variant
  Set Msg = Item
  If (Msg.SenderName = "My Fav Sender") And _
   (Msg.Subject = "Profit and Loss Summary"& " " & Format(Date, "MM_DD_YYYY")) And _
   (Msg.Attachments.Count = 1) Then

这承认了今天的日期。我需要代码来识别任何日期(因为有时报告可能比平时晚几天,但我仍然希望宏运行)。

如何为此目的使用通配符日期?

2 个答案:

答案 0 :(得分:2)

为什么不忽略日期,因为你不关心它是什么,只是在主题的开头寻找字符串"Profit and Loss Summary "

InStr(Msg.Subject, "Profit and Loss Summary ") = 1

,如

If Msg.SenderName = "My Fav Sender" And _
    InStr(Msg.Subject, "Profit and Loss Summary ") = 1 And _
    Msg.Attachments.Count = 1 Then

请注意,这会过滤掉Re: Profit and Loss Summary 06_20_2014等回复。如果您不想这样,那么请考虑:

InStr(Msg.Subject, "Profit and Loss Summary ") <> 0

答案 1 :(得分:1)

VB / VBA中的Like运算符可以轻松使用通配符字符串比较。

更改行

(Msg.Subject = "Profit and Loss Summary"& " " & Format(Date, "MM_DD_YYYY")) And _

(Msg.Subject Like "Profit and Loss Summary ##_##_20##") And _

这将运行任何“损益摘要”电子邮件的其余代码,后跟任何与#匹配的数字模式(这将允许所有相关日期,但也有一些数字,如99_99_2099 - 更强大的方法需要更改If语句的结构,测试提取的子字符串的日期转换等等,在我看来,您更喜欢最简单但最合适的方法。