如何设置带有动态日期的电子邮件模板,或者创建一个宏以插入日期? [英] How do I set up an email template with dynamic dates or alternatively, create a macro to insert a date?

查看:129
本文介绍了如何设置带有动态日期的电子邮件模板,或者创建一个宏以插入日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个每周发送的报告的模板,在四个不同的位置(正文中的三个,主题行中的一个)是对应于上周星期一的日期. (基本上,这是我要报告的一周的星期"日期,通常是前一周.)

I have a template for a report I send out every week and in four separate places (three in the body, one in the subject line) is a date that corresponds to the Monday of the prior week. (Basically, it's a "week of" date for the week I'm reporting, which is always the previous week.)

我想在光标所在的位置添加一个日期. (如果我能以某种方式在模板中为我想要添加日期的位置设置书签,那会更好.)我已经对插入文本进行了很多研究,而我在示例中也遇到了同样的两个问题已经发现:

I want to add a date to where I have the cursor. (If I could somehow set up bookmarks in the template for where I want the date added, that would be even better.) I've done a lot of research on inserting text and I keep running into the same two problems with the examples I've been finding:

  1. 我可以编写一个宏,该宏将打开一条新消息并填充各个区域(主题行,正文等),但是我无法让该宏仅对我已经拥有的消息起作用打开.

  1. I can write a macro that will open up a new message and populate various areas (subject line, body, etc.), but I can't get that macro to work on just a message that I already have open.

对于我尝试过的所有实际在已打开的消息上运行的示例,我只能获取它以将文本添加到正文中.我希望创建一些简单的东西,例如Word的用法:

For all of the examples I've tried that actually run on the message that I already have open, I can only get it to add text to the body. I was hoping to create something simple like how Word does:

Selection.TypeText Text:="Hello!"

这些方法都不适合我.

*编辑#1:当然,在发布此内容后,我发现我发现的一种解决方案之一就是简单地将文本添加到光标现在可以工作的地方.

*edit #1: Of course, right after I post this, I find that one of the solutions I found to simply add text to where the cursor is now works.

TypeName(Application.ActiveWindow) = "Inspector" Then
    SendKeys Format(Now, "MMMM dd, yyyy")
    DoEvents
End If

如果我坚持使用这种方法,我只需要知道如何设置它,以代替今天的日期,而是插入前一周星期一的日期(我并不总是在同一天运行报表,因此我不能仅仅告诉它做一些简单的事情,例如从今天的日期减去八天.我还想知道是否可以通过执行查找/替换之类的操作告诉它将该日期插入多个位置.

If I stick with this approach, I just need to know how to set it up so that instead of today's date, it inserts the date from the prior week's Monday (I don't always run the report on the same day, so I can't just tell it to do something simple like subtract eight days from today's date). I'd also like to know if I can tell it to insert that date into multiple places by doing something like a find/replace.

*编辑#2:我也遇到了一个很好的例子,可以快速找到/替换身体.唯一的问题是它完全剥离了所有和所有格式,包括表格,颜色等.

*edit #2: I've also come across a decent example of a quick find/replace for the body. The only problem with it is that it completely strips out any and all formatting, including tables, colors, etc.

Dim Insp As Inspector
Dim obj As Object

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.Body = Replace(obj.Body, "xxxxxxxxxx", Format(Now - 8, "MMMM dd, yyyy"))

    Set obj = Nothing
    Set Insp = Nothing

(您可能还注意到,我在日期格式中添加了-8.我发现如果无法始终将其添加到我要查找的确切日期中,则至少可以将其关闭.)

(You may also notice I added a -8 to the date format. I figured that if I can't get it to always add the exact date I'm looking for, I can at least get it close.)

推荐答案

我终于想出了如何插入所需的确切日期,并将其简化为一个简单的宏,用于打开模板并更改日期.正文和主题行到上一周星期一的日期.由于此解决方案可以完成我最初希望完成的所有工作,因此我认为应该创建一个新的答案.我担心删除或编辑以前的答案,因为有人可能仍然认为其信息有用.事不宜迟,这是我现在使用的代码:

I finally figured out how insert the exact date I need and I've got it simplified down to one easy macro that opens the template and changes the dates in the body and subject line to the date from the prior week's Monday. Since this solution does everything I was originally looking to accomplish, I figured I should create a new answer. I'm apprehensive about deleting or editing the previous answer because someone might still find its information useful. Without further ado, here is the code I'm now using:

Sub ReportProduction()
Dim StartDay_of_LastWeek As String
Dim Insp As Inspector
Dim obj As Object
Dim myTemplate As Outlook.MailItem
StartDay_of_LastWeek = Format(GetWeekStartDate(CDate(Now - 7), vbMonday), _
    "MMMM dd, yyyy")
Set myTemplate = Application.CreateItemFromTemplate(Environ("Appdata") _
    & "\Microsoft\Templates\ReportProduction.oft")

    myTemplate.Display

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.HTMLBody = Replace(obj.HTMLBody, "xxxxxxxxxxxxxxx", StartDay_of_LastWeek)
    obj.Subject = Replace(obj.Subject, "xxxxxxxxxxxxxxx", StartDay_of_LastWeek)

    Set obj = Nothing
    Set Insp = Nothing

End Sub

我还需要添加以下功能:

I also needed to add the following function:

Function GetWeekStartDate(ByVal strDate, _
    Optional ByVal lngStartDay As Long = 2) As String
GetWeekStartDate = DateAdd("d", -Weekday(CDate(strDate), _
    lngStartDay) + 1, CDate(strDate))
End Function

这篇关于如何设置带有动态日期的电子邮件模板,或者创建一个宏以插入日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆