【问题标题】:Remove an email from outbox and re-edit从发件箱中删除电子邮件并重新编辑
【发布时间】:2021-12-28 09:20:27
【问题描述】:

我有 VBA 代码可以将消息发送延迟五分钟。

Dim obj As Object
Dim Mail As Outlook.MailItem
Dim WkDay As Integer
Dim MinNow As Integer
Dim SendHour As Integer
Dim SendDate As Date
Dim SendNow As String
Dim UserDeferOption As Integer


Function getActiveMessage() As Outlook.MailItem
Dim insp As Outlook.Inspector
If TypeOf Application.ActiveWindow Is Outlook.Inspector Then
    Set insp = Application.ActiveWindow
End If
If insp Is Nothing Then
    Dim inline As Object
    Set inline = Application.ActiveExplorer.ActiveInlineResponse
    If inline Is Nothing Then Exit Function
    Set getActiveMessage = inline
Else
    Set insp = Application.ActiveInspector
    If insp.CurrentItem.Class = olMail Then
        Set getActiveMessage = insp.CurrentItem
    Else
        Exit Function
    End If
End If
End Function


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
SendDate = Now()
SendHour = Hour(Now)
MinNow = Minute(Now)
Set obj = getActiveMessage()
If obj Is Nothing Then
    'Do Nothing'
Else
    If TypeOf obj Is Outlook.MailItem Then
        Set Mail = obj
        SendMin = 5
        SendDate = DateAdd("n", SendMin, SendDate)
        Mail.DeferredDeliveryTime = SendDate
    End If
End If
Exit Sub
End Sub

我需要一种方法来阻止该项目的发送。我们无法删除它并重新开始,因为电子邮件需要很长时间才能撰写并且非常详细。

我想在 Outlook 365 的功能区或上下文菜单中添加一个按钮,以重新打开电子邮件进行编辑并停止延迟发送。

我明白了

找不到对象

Sub MoveEmail()

Dim OutboxFolder As Outlook.Folder
Set OutboxFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox)
Set MoveFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Drafts")

Dim CurrentItem As Object
        
For Each CurrentItem In OutboxFolder.Items
    CurrentItem.Move MoveFolder
Next CurrentItem

End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    整理好了,给其他人...

     Sub MoveEmail()
    
          Dim myNamespace As Outlook.NameSpace
          Set myNamespace = Application.GetNamespace("MAPI")
          Set OutboxFolder = myNamespace.GetDefaultFolder(olFolderOutbox)
          Set MoveFolder = myNamespace.GetDefaultFolder(olFolderDrafts)
    
          Dim CurrentItem As Object
                
          For Each CurrentItem In OutboxFolder.Items
    
                CurrentItem.Move MoveFolder
    
          Next CurrentItem
    
     End Sub
    

    【讨论】:

    • 不要使用“for each”循环遍历您正在修改的集合(通过调用Move)。使用向下循环from Count to 1 step -1
    • @Dimitry 我想了解为什么我不应该尽可能使用 foreach 循环。 from Count 是如何工作的?
    • 因为您正在修改您正在迭代的集合,从而导致您的代码跳过一些条目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多