【问题标题】:Excel VBA, how to Reply to a specific email messageExcel VBA,如何回复特定的电子邮件
【发布时间】:2015-02-20 20:28:15
【问题描述】:

我每个星期三都会收到一封来自特定发件人的邮件。这封电子邮件的主题有时会发生变化

主题“暴露声明 - COB 20150217”的示例 #1

主题“Margin Notice COB 2015-Feb-10”的示例 #2

发件人附加的日期是我收到邮件的前一天。

我有以下代码,它可能会搜索该电子邮件,然后使用自定义正文文本回复它,但我无法让代码在主题中找到具有该日期的特定消息。

有没有办法通过主题以外的其他参数进行搜索?

Sub ReplyMail_No_Movements()

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim SigString As String
Dim Signature As String
Dim i As Integer

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1

SigString = Environ("appdata") & _
                "\Microsoft\Signatures\MCC.txt"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

    On Error Resume Next

For Each olMail In Fldr.Items
If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then 'where date is a date that changes every wednesday
With olMail.Reply
        .to = "email1@domain.com;email2@domain.com"
        .CC = "email3@domain.com;email4@domain.com"
        .Body = "Dear All," & Chr(10) & _
        Chr(10) & "we agree with your portfolio here attached and according to it we see no move for today." & _
        Chr(10) & "        Best Regards." & _
        Chr(10) & _
        Chr(10) & Signature
        .Display
    End With
i = i + 1
End If
Next olMail
End Sub

编辑: 我从

更改了此代码位
If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then

If olMail.SenderEmailAddress = "email1@gdomain.com" And olMail.ReceivedTime = Now() Then

但它不起作用......

这是唯一能让我找到确切消息的搜索组合(SenderEmailAddressthat 和 ReceivedTime)...

【问题讨论】:

  • 确保您已完成How to automate Outlook from another program 文章中描述的所有步骤。
  • 您可以访问和过滤 MaiItem 类的任何属性(这里是完整列表):msdn.microsoft.com/en-us/library/… 但您没有指定您要过滤的具体内容。顺便说一句:您最近的编辑完全改变了问题的意义,所以答案没有任何意义。您应该已将新代码粘贴到原始问题下方。
  • 这就是为什么我问把更新的代码放在哪里......我搞砸了...... :(
  • 第二部分 olMail.ReceivedTime = Now() 因为您试图过滤在运行宏时收到的消息。如果您想要当天的消息,那么 olMail.ReceivedTime&gt;Date() 应该可以工作
  • 是的,谢谢。如果发件人在当天第一封邮件之后给我发了另一封邮件怎么办? :)

标签: excel vba email outlook reply


【解决方案1】:

你应该使用:工具->参考。找到Microsoft Outlook 15.0 Object Library,查看并关闭窗口。

【讨论】:

  • 哇,谢谢...这里是真正的菜鸟。有用。我现在只需要添加使用自定义文本回复电子邮件的代码......有什么想法吗?
  • olMail.ReplyAll 返回一个 MailItem 对象,然后您可以设置回复的各种属性并发送它。如果您未将 Outlook 配置为允许以编程方式访问,Outlook 将对某些操作发出安全警告。
  • 我设法获得了正确的代码来回复自定义正文、自定义收件人和自定义主题,但它错过了原始附件。是否有代码来维护回复的原始附件?
  • 您需要从原始邮件中迭代所有附件,保存并添加到回复邮件中。示例代码可以在这里找到:slipstick.com/outlook/email/reply-replyall-attachments
  • 再次感谢,明天看看,让你知道。感恩千里!
【解决方案2】:

或者只是使用后期绑定

Const olFolderInbox = 6
Sub Test()

Dim olApp As Object
Dim olNs As Object
Dim Fldr As Object
Dim olMail
Dim i As Long

Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1

For Each olMail In Fldr.Items
    If InStr(olMail.Subject, "email message object text") <> 0 Then
    olMail.Display
    olMail.ReplyAll
i = i + 1
End If
Next olMail
End Sub

【讨论】:

  • 您好,感谢您的回复。我用上一个答案解决了这个问题,但今天我不得不将早期绑定转换为后期绑定,因为必须运行代码的不同计算机之间存在兼容性问题。所以我发现你的回答非常有用。就一个问题。你能解释一下Const olFolderInbox = 6的含义吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-06-24
  • 2019-03-19
  • 1970-01-01
  • 2016-02-17
  • 2022-07-06
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
相关资源
最近更新 更多