【问题标题】:Reply to last email from specific sender in Excel在 Excel 中回复来自特定发件人的最后一封电子邮件
【发布时间】:2021-09-09 11:02:11
【问题描述】:

在 Excel 中,我正在查找一个人的电子邮件地址,然后我想找到最后一封电子邮件(发送或接收)并触发对此电子邮件的回复。此回复由 Excel 中的按钮触发。

Dim a As Integer
Dim objOutlook As Object
Dim objMail As Object
Dim rngBody As Range
Dim rngAttach As Range

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)


EmailStr = "sombody@gmail.com" (dummy replacement for my find the email adress in excel spreadsheet

然后我需要在 Outlook 中查找发往/发自该地址的电子邮件,然后回复最新的。

我所做的是向此人发送一封新电子邮件,但不知道如何查找和回复

 With objMail
        .To = EmailStr
        .CC = AMEmail
        .Subject = TitleMail
           .HTMLBody = BodyStr & Signature
        .ReadReceiptRequested = True
    
        .Display 'Instead of .Display, you can use .Send to send the email _
                    or .Save to save a copy in the drafts folder
        

    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngBody = Nothing
    Set rngAttach = Nothing

更新:仍在挣扎,但目前没有更多崩溃。我现在卡在这里的地方:

Private Sub CommandButton2_Click()

Dim olApp As Object
Dim olNs As Object
Dim olFldr As Object

Dim olItems As Object
Dim olItemReply As Object
Dim i As Long

Dim emailStr As String
Dim filter As String

Set olApp = CreateObject("Outlook.Application")

Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(6) ' olFolderInbox
Debug.Print "olFldr: " & olFldr

emailStr = "sombody@gmail.com" '(email address in Excel spreadsheet)
Debug.Print "emailStr: " & emailStr

Set olItems = olFldr.Items
Debug.Print olItems.Count
'finds all 19 items in my inbox with msgbox(olItems.count)


filter = "[SenderEmailAddress] = '" & emailStr & "'"
Debug.Print filter

Set olItems = olFldr.Items.Restrict(filter)
Debug.Print olItems.Count

'finds 0 items now ??? why....

End sub

【问题讨论】:

  • 您有 Excel 中的所有电子邮件吗?或者您是否必须使用其他应用程序(例如 Outlook 或 GMail)来查找以前的电子邮件?
  • 我想从 excel 中获取电子邮件地址,并在 Outlook 中找到该人的最后一封电子邮件。应该澄清问题
  • 你应该用更多的细节来更新你的问题。并包括您正在使用的方法/工具。你在 Excel 中使用 VBA 吗?你可以为你的 Outlook 编写 VBA (Alt-F11) 吗?
  • 对于收到的邮件,一种可能是.Restrict on SenderEmailAddressstackoverflow.com/a/42547062/1571407。对于后期绑定,您必须将所有 Outlook 声明为 Object 并将 olFolderInbox 更改为 6 并将 olMail 更改为 43。

标签: excel vba outlook reply


【解决方案1】:

回复已知文件夹中最近收到的邮件。

Option Explicit' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Private Sub replyToSenderEmailAddress()

' Reply to most recently received mailitem in specified folder

' Late binding - reference to Outlook Object Library not required

Dim olApp As Object
Dim olNs As Object
Dim olFldr As Object

Dim olItems As Object
Dim olItemReply As Object
Dim i As Long

Dim emailStr As String
Dim filter As String

Set olApp = CreateObject("Outlook.Application")

Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(6) ' olFolderInbox
Debug.Print "olFldr: " & olFldr

emailStr = "sombody@gmail.com" '(email address in Excel spreadsheet)
Debug.Print "emailStr: " & emailStr

Set olItems = olFldr.Items
Debug.Print olItems.Count

filter = "[SenderEmailAddress] = '" & emailStr & "'"
Debug.Print filter

Set olItems = olFldr.Items.Restrict(filter)
Debug.Print olItems.Count

olItems.Sort "[ReceivedTime]", True

For i = 1 To olItems.Count
    Debug.Print olItems(i).ReceivedTime
    If olItems(i).Class = 43 Then
        Set olItemReply = olItems(i).Reply
        olItemReply.Display
        Exit For
    End If
Next

End Sub

【讨论】:

  • 感谢 Niton 再次帮助我,我尝试了您的新代码,起初它有效,但后来我发现它只有在我要查找的电子邮件地址是最新的情况下才有效在我的收件箱中。对于我收到邮件的任何其他电子邮件地址,没有任何反应。知道出了什么问题吗?我如何查看 olItems 在“限制(过滤器)”之后包含的内容,为什么它不会在我的收件箱中找到除前 1 封之外的任何其他电子邮件?
  • 我添加了一个 msgbox(olItems.Count) 以查看它在过滤器之前找到的内容,它显示 21。我怀疑这不正确,因为我的 Outlook 中有几千封电子邮件
  • 此方法处理一个文件夹。除非您从其他来源了解.AdvancedSearch,否则您可以使用蛮力,分别搜索每个文件夹。在stackoverflow.com/questions/2272361/… 演示的收件箱下的每个文件夹的技术。这会比较慢。
  • 我只想搜索一个主收件箱文件夹。无论如何,您的代码在我的 excel 中导致了严重错误,我花了 2 天时间重建我的文件的副本,因为什么都不能再点击了
【解决方案2】:

这演示了如何创建从电子邮件地址收到的项目的搜索文件夹。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant


Private Sub mailFromEmailAddress()

' Early binding
' Requires reference to Microsoft Outlook XX.X Object Library
Dim objOutlook As Outlook.Application

Dim strSearch As String

Dim strDASLFilter As String
Dim strDASLFilter_option As String
    
Dim strScope As String
Dim strScopeEdit As String
     
Dim objSearch As Search

Dim fldrNm As String

strSearch = "someone@internet.com"

Set objOutlook = CreateObject("Outlook.Application")

' create a searchfolder
Debug.Print
    
'strScope = "'Inbox', 'Deleted'"
strScope = "'Inbox'"
    
Debug.Print "strSearch...........: " & strSearch
    
' https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2007/aa579702(v=exchg.80)
' ***** use "fromemail" for "senderemailaddress" *****
strDASLFilter_option = "fromemail"
Debug.Print "strDASLFilter_option: " & strDASLFilter_option
                
'fldrNm = strDASLFilter_option & " " & strSearch
fldrNm = strSearch
Debug.Print "fldrNm..............: " & fldrNm
                
'strDASLFilter = "urn:schemas:httpmail:" & strDASLFilter_option & " LIKE '%" & strSearch & "%'"
strDASLFilter = "urn:schemas:httpmail:" & strDASLFilter_option & " LIKE '" & strSearch & "'"
Debug.Print "strDASLFilter.......: " & strDASLFilter
                
Debug.Print "strScope............: " & strScope
Set objSearch = objOutlook.AdvancedSearch(scope:=strScope, filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")
                
Debug.Print fldrNm
                
'Save the search results to a searchfolder
objSearch.Save fldrNm
Debug.Print fldrNm & " saved."

' Question 2
'  Reference the saved searchfolder
'  https://stackoverflow.com/questions/55363286/how-do-you-set-a-folder-variable-for-a-search-folder

' Question 3
'  Sort the collection of items in the searchfolder
'  Reply to most recent and appropriate item
    
End Sub

【讨论】:

  • 不确定这如何帮助回复来自联系人 x 的最后一封电子邮件
  • 建议的解决方案可能过多,但由于您没有指定特定文件夹来查找邮件,因此此代码应创建一个包含来自“someone@internet.com”的所有电子邮件项目的搜索文件夹。尝试使用适当的电子邮件地址。 “问题 2”链接演示了如何按名称引用搜索文件夹。您可以.Sort 邮件然后Reply 到最近的邮件。
  • 如果您知道邮件将在特定文件夹中,则有一个更简单的解决方案,网站上有许多示例。您可以引用该文件夹,然后过滤“someone@internet.com”。
  • 我正在尝试开始,但我尝试过的每个示例都在 Dim objOutlook As Outlook.Application 中遇到了问题,编译错误用户定义的类型未定义
  • 请参阅Dim objOutlook As Object 在您的问题中。 stackoverflow.com/questions/50798486/…
猜你喜欢
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2014-10-15
  • 2012-04-26
  • 2019-10-17
相关资源
最近更新 更多