【问题标题】:Debugging Outlook.MailItem调试 Outlook.MailItem
【发布时间】:2020-06-23 08:26:29
【问题描述】:

我已将 Outlook.MailItem 下载到我的Y:\ 文件夹中:

Y:\email.msg

在 Outlook VBA 中,我想在这个项目上测试一个脚本。但是,我不确定如何定义它。

我有以下几点:

Dim testMail As MailItem
Set testMail = Application.CreateItem(olMailItem)

但是如何创建指向我存储的确切项目的链接?


之后,我想使用代码测试将附件存储在此文件中(有时,并不总是会产生损坏的文件):

Public Sub Save_File(MItem As Outlook.MailItem)
    On Error Resume Next

    ' init
    Dim oAttachment As Outlook.Attachment
    Dim folderSave As String
    Dim yyyymmdd As String
    Dim fileName As String
    Dim fileNameFull As String

    ' date @T (midnight 00:00)
    Dim mydate As Date
    mydate = MItem.ReceivedTime

    ' filename and path
    yyyymmdd = get_yyyymmdd_prevday(mydate)


    folderSave = "V:\Operations\"
    fileName = yyyymmdd & "-FileToStore.csv"


    fileNameFull = folderSave & fileName

    For Each oAttachment In MItem.Attachments

        If fileExist(fileNameFull) = False Then
            ' if file does not exist
            oAttachment.SaveAsFile fileNameFull
        End If

    Next

End Sub

还有帮助功能:

Public Function get_yyyymmdd_prevday(mydate As Date) As String
    Dim yyyymmddstr As String
    'Previous Business Date

    Dim yyyy As String
    Dim mm As String
    Dim dd As String


    If Weekday(mydate) = 2 Then
        mydate = mydate - 3
    Else
        mydate = mydate - 1
    End If


    yyyy = Year(mydate)
    mm = Month(mydate)
    dd = Day(mydate)

    If Month(mydate) < 10 Then
        mm = "0" & mm
    End If
    If Day(mydate) < 10 Then
        dd = "0" & dd
    End If

    ' -->
    yyyymmddstr = yyyy & "_" & mm & "_" & dd
    get_yyyymmdd_prevday = yyyymmddstr
End Function

【问题讨论】:

  • 据我所知,以这种方式从下载的邮件项创建对象并不容易。 MailItem 对象适用于应用程序中存在的邮件。如果您进一步解释您要测试的内容,或许还有其他解决方案。
  • 我正在尝试测试一个下载 Outlook 项目附件的 vba 脚本。通常它工作正常,但这次它存储了一个损坏的文件。所以我想调试一下代码看看为什么会这样。
  • 也许如果你分享你的 vba...
  • 添加了调试存储文件时损坏文件的部分的代码。但是,它并不总是损坏存储它,只是偶尔。因此,为什么我想了解它何时发生,因此我需要知道如何调试它
  • ...大概只有某些邮件? (如,可重现?)

标签: vba outlook


【解决方案1】:

要引用 .msg 文件,请使用 OpenSharedItem

Option Explicit

Private Sub Reference_msg_file()

    Dim testMailPathFile As String
    Dim testMail As MailItem

    testMailPathFile = "Y:\email.msg"

    Set testMail = Session.OpenSharedItem(testMailPathFile)
    'testMail.Display

    Save_File testMail

ExitRoutine:
    Set testMail = Nothing

End Sub

您已使用On Error Resume Next 禁用调试。删除此行并研究如何使用它,然后再将其应用于任何未来的代码。

【讨论】:

【解决方案2】:

您可以将“On Error Resume Next”更改为“On Error GoTo Handler”。

您可以使用它来调试 Outlook.MailItem

更多信息,您可以参考这个链接:

On Error Statement (Visual Basic)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2011-09-30
    • 2015-04-20
    • 2015-10-05
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多