调试Outlook.MailItem

时间:2018-10-16 07:31:44

标签: vba outlook outlook-vba

我已将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

2 个答案:

答案 0 :(得分: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的调试。删除此行,并研究如何使用它,然后再将其应用于任何将来的代码。

答案 1 :(得分:0)

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

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

有关更多信息,您可以参考以下链接:

On Error Statement (Visual Basic)