从转发的电子邮件VBA宏中删除自动签名

时间:2015-02-24 21:39:20

标签: outlook-vba outlook-2010

新手Outlook VBA。中级Excel VBA。 Windows 7专业版,Outlook 2010

我有一个脚本从一个自动转发所有传入电子邮件的规则运行。我需要它作为规则,否则当Outlook加载时它不会转发队列中的邮件。

我希望在转发邮件时删除默认签名。由于回复是“空白”,因此不必附加sig。我发现一些代码可以在MSDN站点的Outlook 2007中运行。它不编译任何错误,不执行任何错误。我在VBA中引用了MS Word。但转发的电子邮件都附有签名。

我不能删除签名,因为我需要它来回复。签名的开关用于回复和转发邮件。

以下是代码:

Option Explicit
Sub Incoming3(MyMail As MailItem)
    Dim strID As String
    Dim strSender As String
    Dim StrSubject As String
    Dim objItem As Outlook.MailItem
    Dim myItem As Outlook.MailItem

    strID = MyMail.entryID
    Set objItem = Application.Session.GetItemFromID(strID)

    strSender = objItem.SenderName
    StrSubject = objItem.Subject
    StrSubject = strSender + ": " + StrSubject
    objItem.Subject = StrSubject
    objItem.AutoForwarded = False

    Set myItem = objItem.Forward

    myItem.Recipients.Add "bcc.hwb@gmail.com"
    myItem.DeleteAfterSubmit = True

    Call DeleteSig(objItem)

    myItem.Send

    Set myItem = Nothing
    Set objItem = Nothing

End Sub


Sub DeleteSig(msg As Outlook.MailItem)

    Dim objDoc As Word.Document
    Dim objBkm As Word.Bookmark

    On Error Resume Next

    Set objDoc = msg.GetInspector.WordEditor

    Set objBkm = objDoc.Bookmarks("_MailAutoSig")

    If Not objBkm Is Nothing Then
        objBkm.Select
        objDoc.Windows(1).Selection.Delete
    End If

    Set objDoc = Nothing
    Set objBkm = Nothing

End Sub

非常感谢任何有关Outlook或VBA代码的帮助。

2 个答案:

答案 0 :(得分:0)

在DeleteSig中处理错误的邮件。

myItem.DeleteAfterSubmit = True

Call DeleteSig(myItem)

myItem.Send

编辑2015 02 26

Debugging VBA Code

Private Sub Incoming3_test()
' Open a mailitem then click F8 repeatedly from this code    
    Dim currItem As MailItem
    Set currItem = ActiveInspector.currentItem
    Incoming3 currItem        
End Sub

Sub Incoming3(MyMail As MailItem)        
    Dim myItem As Outlook.MailItem        
    Set myItem = MyMail.Forward            
    myItem.Subject = MyMail.senderName & ": " & MyMail.Subject
    myItem.Recipients.Add "bcc.hwb@gmail.com"
    myItem.DeleteAfterSubmit = True

    myItem.Display  ' If you are using F8 you can
                    '   view the action taken in DeleteSig.
                    '  Delete the line later.

    Call DeleteSig(myItem)
    'myItem.Send

    Set myItem = Nothing

End Sub

Sub DeleteSig(msg As Outlook.MailItem)

    Dim objDoc As Word.Document
    Dim objBkm As Word.Bookmark

    On Error Resume Next    '<--- Very bad without On Error GoTo 0
    Set objDoc = msg.GetInspector.WordEditor
    Set objBkm = objDoc.Bookmarks("_MailAutoSig")
    On Error GoTo 0

    If Not objBkm Is Nothing Then
        objBkm.Select                       ' <--- This is where the action starts.
        objDoc.Windows(1).Selection.Delete
    End If

    Set objDoc = Nothing
    Set objBkm = Nothing

End Sub

编辑2015 02 26 - 结束

答案 1 :(得分:0)

当您为规则运行分配VBA宏子集时,您将获得MailItem对象的实例。例如:

 Sub Incoming3(MyMail As MailItem)

MyMail对象表示您应在代码中使用的传入电子邮件。但是我看到你得到了一个新的实例:

strID = MyMail.entryID
Set objItem = Application.Session.GetItemFromID(strID)

没有必要这样做。在代码中使用MyMail对象。

另外,我看到以下代码:

 Set objBkm = objDoc.Bookmarks("_MailAutoSig")

尝试在调试器下运行代码,看看是否可以找到书签。如果没有此类书签,您需要在正文中搜索文本中的第一个条目发件人:,并删除该关键字之前的所有内容。

最后,您可能会发现MSDN中的Getting Started with VBA in Outlook 2010文章很有用。