【发布时间】:2019-02-13 01:44:18
【问题描述】:
下面的代码不执行回复所有属性,因此,我无法编辑电子邮件的正文并保持电子邮件链的对话。
我认为最好的选择是使用 Application.advancesearch,因为它通过搜索所有文件夹为您提供最新的电子邮件。但我不知道如何通过 Excel 运行它。
目标:
1) 在收件箱和子文件夹(多个)和已发送邮件文件夹中搜索所选“主题”的最新电子邮件
2) 选择最新的邮件并全部回复
Sub ReplyMail()
' Variables
Dim OutlookApp As Object
Dim IsOutlookCreated As Boolean
Dim sFilter As String, sSubject As String
Dim SentTime As Long
Dim IndoxTime As Long
Dim olEmailIndox As Outlook.MailItem
Dim olEmailSent As Outlook.MailItem
' Get/create outlook object
On Error Resume Next
Set OutlookApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlookApp = CreateObject("Outlook.Application")
IsOutlookCreated = True
End If
On Error GoTo 0
Set olEmailIndox = OutlookApp.CreateItem(olMailItem)
Set olEmailSent = OutlookApp.CreateItem(olMailItem)
' Restrict items
sSubject = "Subject 1"
sFilter = "[Subject] = '" & sSubject & "'"
' Main
With OutlookApp.Session.GetDefaultFolder(olFolderSentMail).Items.Restrict(sFilter)
If .Count > 0 Then
.Sort "ReceivedTime", True
Set olEmailSent = .Item(1)
SentTime = olEmailSent.SentOn
End If
End With
With OutlookApp.Session.GetDefaultFolder(olFolderInbox).Items.Restrict(sFilter)
If .Count > 0 Then
.Sort "ReceivedTime", True
Set olEmailInbox = .Item(1)
InboxTime = olEmailInbox.ReceivedTime
End If
End With
If SentTime > InboxTime Then
With olEmailSent
.ReplyAll
.Display
'.body
'.Send
End With
Else
With olEmailInbox
.ReplyAll
.Display
'.body
'.Send
End With
End If
' Quit Outlook instance if it was created by this code
If IsOutlookCreated Then
OutlookApp.Quit
Set OutlookApp = Nothing
End If
End Sub
【问题讨论】:
-
有两个主要问题: ...创建一个minimal reproducible example,以便我们可以帮助您解决一个特定问题
-
@ScottHoltzman 感谢您的指导。有什么办法可以编辑这个或者我应该创建新帖子吗?
-
你可以edit点击上面的编辑或我为你做的链接。
-
@ScottHoltzman:希望现在问题更清楚了。