【问题标题】:Getting the incoming email in outlook via VBA通过 VBA 在 Outlook 中获取传入的电子邮件
【发布时间】:2012-12-10 20:23:43
【问题描述】:

我正在尝试将通过 Outlook 传入的消息保存到我的本地文件系统。 我到目前为止的代码是:

Sub save_to_dir(Item As Outlook.MailItem)
 'the mail we want to process
Dim objItem As Outlook.MailItem
 'question for saving, use subject to save
Dim strPrompt As String, strname As String
 'variables for the replacement of illegal characters
Dim sreplace As String, mychar As Variant, strdate As String
 'put active mail in this object holder 

Set objItem = Outlook.ActiveExplorer.Selection.Item(1)


 'check if it's an email ... need to take a closer look cause
 'gives an error when something else (contact, task) is selected
 'because objItem is defined as a mailitem and code errors out
 'saving does work, if you take care that a mailitem is selected
 'before executing this code
 mypath = "c:\temp\outlook\"
If objItem.Class = olMail Then
    ' check on subject
    If objItem.Subject <> vbNullString Then
        strname = objItem.Subject
    Else
        strname = "No_Subject"
    End If
    strdate = objItem.ReceivedTime
     'define the character that will replace illegal characters
    sreplace = "_"
     'create an array to loop through illegal characters (saves lines)
    For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
         'do the replacement for each character that's illegal
        strname = Replace(strname, mychar, sreplace)
        strdate = Replace(strdate, mychar, sreplace)
    Next mychar
     'Prompt the user for confirmation
    'strPrompt = "Are you sure you want to save the item?"
    'If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
        objItem.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG
    ' Else
    '    MsgBox "You chose not to save."
    'End If
End If
End Sub

此代码的问题在于,当您在 Outlook 中选择了一个项目时,当电子邮件进入时,它会使用所选项目而不是收到的邮件

我怎样才能收到收到的电子邮件?

谢谢

编辑

调试完这一行 设置 objItem = Outlook.ActiveExplorer.Selection.Item(1)

我发现 Outlook.ActiveExplorer.Selection.Item(1) 已经收到了当前的电子邮件,但是当我在执行该行后查看 objItem 时 objItem 的值是当前在 Outlook 中选择的电子邮件,而不是传入的电子邮件。

有什么想法吗?

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    可以在这里找到一篇好的参考文章:http://www.outlookcode.com/article.aspx?id=62

    我已将示例一改编如下:

    Sub save_to_dir_test1(mymail As MailItem)
    Dim strID As String
    Dim objMail As Outlook.MailItem
    
    strID = mymail.EntryID
    Set objMail = Application.Session.GetItemFromID(strID)
    mypath = "c:\temp\outlook\"
    strdate = objMail.ReceivedTime
    If objMail.Subject <> vbNullString Then
            strname = objMail.Subject
       Else
            strname = "No_Subject"
    End If
    sreplace = "_"
    'create an array to loop through illegal characters (saves lines)
    For Each mychar In Array("/", "\", ":", "?", Chr(34), "<", ">", "¦")
    'do the replacement for each character that's illegal
       strname = Replace(strname, mychar, sreplace)
       strdate = Replace(strdate, mychar, sreplace)
    Next mychar
    objMail.SaveAs mypath & strname & "--" & strdate & ".msg", olMSG
    
    Set objMail = Nothing
    End Sub
    

    【讨论】:

      【解决方案2】:

      你可以在outlook中为收到的邮件设置一个“规则”,在操作中选择“运行脚本”,选择下面的子

      并把下面的sub放到thisoutlooksession模块的一个模块中

      Sub testing(MyMail As MailItem)
       MyMail.SaveAs ' your path here
      end sub
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-09
        • 2013-09-07
        • 1970-01-01
        • 2011-12-04
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多