1
votes

Using VB.net, I need to read Outlook emails and Parse various data and eventually place it into SQL. Once done I need to move the email item out of the Inbox to a Child PST file, which I hope to create once a week, under the existing PST named "done\today's date". The PST is not located in the Inbox.

I have spent months trying to learn and cobbled together code. I am now able to read the email items in the Inbox. I have just started the coding for Parsing of the data and I'll worry about moving it to SQL later. My problem for today is properly setting up the Destination PST ("done\today"). And probably the proper syntax to preform the move. An abbreviated version of the code I use is below:

Imports Microsoft.Office.Interop

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        Dim olApp As Outlook.Application = New Outlook.Application()
        Dim ns As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        Dim selectFolder As Outlook.MAPIFolder = Nothing
        Dim mi As Outlook.MailItem = Nothing
        Dim olDestinationFolder As Outlook.MAPIFolder = Nothing
        selectFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        olDestinationFolder = ns.GetDefaultFolder(Outlook.Folder.ns.Folders("\\[email protected]\done\4 12 2019")

 ~~~~~Code set up data table to prepare parsing - NOT SHOWN HERE~~~~~~

       For Each item As Object In selectFolder.Items
           ~~~~`Code for parsing - NOT SHOWN HERE ~~~~~~

            dt.Rows.Add(dr)
            i += 1
            item.Move(olDestinationFolder) 'after parsing move mail item
        Next
        Me.DataGridView1.DataSource = dt  'display parsed data, just to see
     'Closing apps and files
        olApp = Nothing
        ns = Nothing
        selectFolder = Nothing
        mi = Nothing
        olDestinationFolder = Nothing

        MsgBox("Ready to close") ' A msg just so I know code has run
    End Sub
End Class

I expect the eMail message to be MOVED to the PST folder (deleted from Inbox and added to PST folder.) The program throws an error:

'ns' is not a member of 'Folder' ". I'm not surprised as I took a WAG at line 12 "olDestinationFolder = ns.GetDefaultFolder(Outlook.Folder.ns.Folders("\[email protected]\done\4 12 2019"))

I don't know how to set the Destination folder which is causing the error. Or the proper way to move the mail item to this folder.

1

1 Answers

1
votes

The line

olDestinationFolder = ns.GetDefaultFolder(Outlook.Folder.ns.Folders("\\[email protected]\done\4 12 2019")

must be

olDestinationFolder = ns.Folders("[email protected]\done\4 12 2019")

Assuming a PST file named "[email protected]\done\4 12 2019" was already added to the profile (which I doubt, since you cannot have a "\" in a file name). If not, you can programmatically add a new PST file using Namespace.AddStoreEx.

Also note that you should not use "for each" when you are changing the collection (ba calling item.Move) - use a down loop (for i = Items.Count to 1 step -1).