0

I have downloaded an Outlook.MailItem to my Y:\ folder at the location:

Y:\email.msg

In Outlook VBA, I want to test a script on this item. However, I am not sure how to define it.

I have the following:

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

But how do I make the link to the exact item I stored?


After that I want to test storing the attachment in this file using the code (which sometimes, not always produces a corrupted file):

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

And the help function:

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
WJA
  • 6,676
  • 16
  • 85
  • 152
  • As far as I know there's no easy way to create an object from a downloaded mail item in this way. The [`MailItem`](https://learn.microsoft.com/office/vba/api/outlook.mailitem) object is intended for mail that exists within the application. Perhaps if you explain further what you're trying to test, there are alternate solutions. – ashleedawg Oct 16 '18 at 07:50
  • I am trying to test a vba script that downloads the attachment of an Outlook Item. Usually it works fine, but this time it stored a corrupted file. So I want to debug the code to see why this happens. – WJA Oct 16 '18 at 07:51
  • perhaps if you share your vba... – ashleedawg Oct 16 '18 at 07:54
  • Added the code to debug the part that corrupts the file when storing it. However, it does not always corrupt store it, just once in a while. Thus why I wanted to understand when it happens, thus I need to know how to debug it – WJA Oct 16 '18 at 08:00
  • ...only certain mail messages presumably? (as in, reproducible?) – ashleedawg Oct 16 '18 at 08:16
  • Taking certain mail messages is already defined, there is no problem with that. Basically I filter on the receiver and subject line and if it has an attachment. – WJA Oct 16 '18 at 09:02
  • is the attachment always `csv`? is the file okay before saving it? – 0m3r Oct 16 '18 at 20:23

2 Answers2

1

To reference a .msg file there is 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

You have disabled debugging with On Error Resume Next. Remove this line and investigate how to make use of it before applying it to any future code.

niton
  • 8,771
  • 21
  • 32
  • 52
  • How to apply On Error Resume Next https://stackoverflow.com/a/31753321/1571407 . Let the editor pop open rather than hide unforeseen errors. – niton Oct 28 '18 at 14:39
0

You could change “On Error Resume Next ” to “On Error GoTo Handler”.

You could use this to debug Outlook.MailItem

For more information, you could reference this link:

On Error Statement (Visual Basic)

Alina Li
  • 884
  • 1
  • 6
  • 5