【问题标题】:How Can I Use Datediff in an Outlook Macro to calculate number of days left to end of month?如何在 Outlook 宏中使用 Datediff 计算到月底的天数?
【发布时间】:2019-12-05 18:12:19
【问题描述】:

所以 - 在 Outlook 中编写一个宏,我想计算当月剩余的天数(或者更好的工作日)。此代码生成运行时错误 5:无效的过程调用或参数。想法?

Public Sub MetricsMail()
Dim MyEmail As MailItem
Dim DaysRemain As Long
Dim EndDate As Date
EndDate = DateSerial(Year(Date), Month(Date) + 1, 0)
DaysRemain = VBA.DateDiff(d, Now(), EndDate)

Set MyEmail = CreateItem(olMailItem)
With MyEmail
    .To = "Metrics"
    .Subject = Format(Now(), "mmmm dd, yyyy") & " Daily Metrics"
    .HTMLBody = DaysRemain & " Remain in this month"
End With

MyEmail.Display

End Sub

【问题讨论】:

  • 错字。 d 引号。 DaysRemain = VBA.DateDiff("d", Now(), EndDate)

标签: vba outlook


【解决方案1】:

只有一点点变化:

Public Sub MetricsMail()
Dim MyEmail As MailItem
Dim DaysRemain As Long
Dim EndDate As Date
EndDate = DateSerial(Year(Date), Month(Date) + 1, 0)
DaysRemain = Date()-EndDate

Set MyEmail = CreateItem(olMailItem)
With MyEmail
    .To = "Metrics"
    .Subject = Format(Now(), "mmmm dd, yyyy") & " Daily Metrics"
    .HTMLBody = DaysRemain & " Remain in this month"
End With

MyEmail.Display

End Sub

周末愉快!最大

【讨论】:

  • 有时会以不同的方式工作。我翻转了 EndDate 和 Date 的顺序以使符号正确,但除此之外,代码有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
相关资源
最近更新 更多