发送电子邮件时检查附件的宏

时间:2016-12-02 14:23:11

标签: vba outlook outlook-vba email-attachments

我发现了多个宏,当您使用“附件”这个词时,会检查是否附加了任何项目。 (或您希望的任何其他字词)在您的电子邮件中,例如以下内容:

Public Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim vList As Variant
Dim answer As Integer
Dim i As Integer

    vList = Array("attached", "attachment")

    If Item.Attachments.Count = 0 Then
        For i = 0 To UBound(vList)
            If InStr(1, LCase(Item.Body), LCase(vList(i)), vbTextCompare) > 0 Then
                answer = MsgBox("There's no attachment, send anayway?", vbYesNo)
                If answer = vbNo Then Cancel = True
                Exit Sub
            End If
        Next i
    End If

End Sub

但是,我只看过检查Item.Body是否包含指定字词的宏。显然,在回复电子邮件时,Item.Body不仅仅是我正在键入的新电子邮件,还包括我正在回复的上一封邮件。对我来说,这是一个有点问题,因为我对应的许多客户都有“附件”这个词。在他们的免责声明中。结果,我经常得到一个警告,即在不必要的时刻没有附件。

有谁知道如何解决这个问题?我尝试了Item对象的不同属性,但无法使其正常工作。

在我看来,没有正确的方法来选择最新的打字文本。

1 个答案:

答案 0 :(得分:2)

大部分时间是第一个"来自:"将是上一条消息的开头。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim vList As Variant
    Dim answer As Integer
    Dim i As Integer

    Dim newMsg As String
    Dim prevMsgStart As Long

    prevMsgStart = InStr(Item.body, "From: ")

    If prevMsgStart = 0 Then
        newMsg = Item.body
    Else
        newMsg = Left(Item.body, prevMsgStart - 1)
    End If

    vList = Array("attached", "attachment")

    If Item.Attachments.count = 0 Then
        For i = 0 To UBound(vList)
            If InStr(1, LCase(newMsg), LCase(vList(i)), vbTextCompare) > 0 Then
                answer = MsgBox("There's no attachment, send anayway?", vbYesNo)
                If answer = vbNo Then Cancel = True
                Exit Sub
            End If
        Next i
    End If

End Sub