Outlook in Visual Basic

Status
Niet open voor verdere reacties.

JanVolker

Gebruiker
Lid geworden
15 apr 2012
Berichten
32
Hey,

Ik ben op zoek naar een code om mijn 'Postvak In' van 'Outlook' te weergeven in mijn 'Visual Basic' applicatie. Op internet heb ik vele code`s gevonden om 'Postvak In' weer te geven in 'Excel', maar ik heb het niet voor elkaar gekregen om de code van 'Excel' om te zetten naar 'Visual Basic'. Weet iemand een script waarmee ik dat kan doen?


Met vriendelijke groet,
Jan Volker
 
Oke, ik heb geen foutmeldingen, maar ik zie ook niks op mijn Form. Hoe zorg ik dat de output op de form wordt getoond?
 
Zet een listbox of een listview op je form en zet dan deze code (of aangepast) onderaan het ophalen van de emails:
Code:
        For i As Integer = 0 To list.Count - 1 Step 1
            ListBox1.Items.Add(list(i).Subject)

            ListView1.Items.Add(list(i).Subject)
        Next

Hiervan zou je dan 1 van de 2 lijntjes code in de for..next moeten verwijderen...


Succes!


PS: Welkom op Helpmij!
 
Mijn code ziet er nu als volgt uit:

Code:
    Public Class OutlookInbox
        Public Shared Function GetMail(ByVal mailBoxName As String) As List(Of Email)

            Dim list As New List(Of Email)()

            Dim app As New Application()

            Dim NS As [NameSpace] = app.GetNamespace("MAPI")

            Dim foundMailBox As Boolean = False
            Dim foundInbox As Boolean = False
            For Each mailboxFolder As MAPIFolder In NS.Folders

                If mailboxFolder.Name = "Mailbox – " + mailBoxName Then

                    foundMailBox = True
                    For Each inboxFolder As MAPIFolder In mailboxFolder.Folders
                        If inboxFolder.Name = "Inbox" Then
                            foundInbox = True
                            For Each item As Object In inboxFolder.Items
                                Dim mailItem As MailItem = TryCast(item, MailItem)
                                If mailItem IsNot Nothing Then
                                    Dim email As New Email()
                                    email.Subject = mailItem.Subject
                                    email.DateReceived = mailItem.ReceivedTime
                                    list.Add(email)

                                End If
                                For i As Integer = 0 To list.Count - 1 Step 1
                                    Form1.ListView1.Items.Add(list(i).Subject)
                                Next
                            Next
                            Exit For
                        End If
                    Next
                    Exit For
                End If
            Next

            If Not foundMailBox Then
                Throw New ApplicationException(String.Format("Mailbox for ‘{0}’ not found", mailBoxName))
            End If
            If Not foundInbox Then
                Throw New ApplicationException(String.Format("Inbox for ‘{0}’ not found", mailBoxName))
            End If
            Return list
        End Function
    End Class
    Public Class Email
        Public Subject As String
        Public DateReceived As DateTime

    End Class

Ik heb een ListView geplaatst op mijn form en 1 column toegevoegd genaamd Subject, maar ik krijg er nog niks in
 
Volgens mij zou het zo moeten werken:
Code:
    Public Class OutlookInbox
        Public Shared Function GetMail(ByVal mailBoxName As String) As List(Of Email)

            Dim list As New List(Of Email)()

            Dim app As New Application()

            Dim NS As [NameSpace] = app.GetNamespace("MAPI")

            Dim foundMailBox As Boolean = False
            Dim foundInbox As Boolean = False
            For Each mailboxFolder As MAPIFolder In NS.Folders

                If mailboxFolder.Name = "Mailbox – " + mailBoxName Then

                    foundMailBox = True
                    For Each inboxFolder As MAPIFolder In mailboxFolder.Folders
                        If inboxFolder.Name = "Inbox" Then
                            foundInbox = True
                            For Each item As Object In inboxFolder.Items
                                Dim mailItem As MailItem = TryCast(item, MailItem)
                                If mailItem IsNot Nothing Then
                                    Dim email As New Email()
                                    email.Subject = mailItem.Subject
                                    email.DateReceived = mailItem.ReceivedTime
                                    list.Add(email)

                                End If
                            Next
                            Exit For
                        End If
                    Next
                    Exit For
                End If
            Next

            If Not foundMailBox Then
                Throw New ApplicationException(String.Format("Mailbox for ‘{0}’ not found", mailBoxName))
            End If
            If Not foundInbox Then
                Throw New ApplicationException(String.Format("Inbox for ‘{0}’ not found", mailBoxName))
            End If

            For i As Integer = 0 To list.Count - 1 Step 1
                 Form1.ListView1.Items.Add(list(i).Subject)
            Next

            Return list
        End Function
    End Class
    Public Class Email
        Public Subject As String
        Public DateReceived As DateTime

    End Class


Maar je moet de functie ook nog aanroepen. En de bedoeling van een functie is dan dat die een waarde teruggeeft: http://msdn.microsoft.com/en-us/library/sect4ck6%28v=vs.80%29.aspx. Dus niet dat je vanuit een functie een listbox/listview gaat vullen.... Daarvoor bestaan er sub's.

MartinJM
 
Ik begrijp dat ik zo iets moet hebben:

Code:
    Function GetMail()
        ListView1.Columns(subject) = Outlook.MailItem.Subject

    End Function

Klopt dat?
 
Volgens mij moet je ongeveer zoiets hebben:

Code:
Imports System
Imports System.Collections.Generic
Imports Microsoft.Office.Interop.Outlook

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Lst As List(Of Email) = OutlookInbox.GetMail("MailboxName")

        For i As Integer = 0 To Lst.Count - 1 Step 1
            ListView1.Items.Add(Lst(i).Subject)
        Next
    End Sub
End Class

Public Class OutlookInbox
    Public Shared Function GetMail(ByVal mailBoxName As String) As List(Of Email)
        Dim list As New List(Of Email)()
        Dim app As New Application()
        Dim NS As [NameSpace] = app.GetNamespace("MAPI")
        Dim foundMailBox As Boolean = False
        Dim foundInbox As Boolean = False
        For Each mailboxFolder As MAPIFolder In NS.Folders
            If mailboxFolder.Name = "Mailbox – " + mailBoxName Then
                foundMailBox = True
                For Each inboxFolder As MAPIFolder In mailboxFolder.Folders
                    If inboxFolder.Name = "Inbox" Then
                        foundInbox = True
                        For Each item As Object In inboxFolder.Items
                            Dim mailItem As MailItem = TryCast(item, MailItem)
                            If mailItem IsNot Nothing Then
                                Dim email As New Email()
                                email.Subject = mailItem.Subject
                                email.DateReceived = mailItem.ReceivedTime
                                list.Add(email)
                            End If
                        Next
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
        If Not foundMailBox Then
            Throw New ApplicationException(String.Format("Mailbox for ‘{0}’ not found", mailBoxName))
        End If
        If Not foundInbox Then
            Throw New ApplicationException(String.Format("Inbox for ‘{0}’ not found", mailBoxName))
        End If
        Return list
    End Function
End Class

Public Class Email
    Public Subject As String
    Public DateReceived As DateTime
End Class

Hierin kan je zien dat ik dit stukje code:
Code:
            For i As Integer = 0 To list.Count - 1 Step 1
                 Form1.ListView1.Items.Add(list(i).Subject)
            Next
ook verplaatst heb.


Ik kan helaas niet checken of dit werkt omdat ik geen outlook gebruik, dus ik kan niet met zekerheid zeggen of het werkt... Wat je ook nog zou kunnen doen is de code voor het ophalen van de emails (van die site) in een helemaal aparte class zetten. Dat maakt de code wel overzichtelijker, maar dat is nu voor mij meer werk om uit te leggen, dus heb ik het zo gedaan :p Als je toch nog wil dat ik (of iemand anders) dat uitlegt kan je dat natuurlijk altijd vragen.

MartinJM
 
Dank je wel, hij doet het nog niet helemaal, maar ik denk dat we er bijna zijn :

Wanneer ik op button1 klik krijg ik de foutmelding: "ApplicationExpection was unhandled. Mailbox for ‘MailboxName’ not found".

Dat verwijst naar:
Code:
           If Not foundMailBox Then
                Throw New ApplicationException(String.Format("Mailbox for ‘{0}’ not found", mailBoxName))
            End If

De code die bij button1 staat is:

Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim Lst As List(Of Email) = OutlookInbox.GetMail("MailboxName")

        For i As Integer = 0 To Lst.Count - 1 Step 1
            ListView1.Items.Add(Lst(i).Subject)
        Next
    End Sub

Ik heb geprobeerd om "MailboxName" te vervangen, maar ik krijg nog steeds een foutmelding.


Wat moet ik bij MailboxName schrijven?
 
Als ik het goed heb moet daar de naam van je mailbox komen te staan...

Misschien werkt het als je outlook open en ingelogd hebt staan (als je dat niet al hebt)...

Of misschien als je het leeg laat ( = Nothing)


Ik weet het verder ook niet omdat ik die code niet heb geschreven... Maar als je de inbox wilt dan denk ik dat het "Inbox" of "inbox" moet zijn....


Succes! MartinJM
 
Nee, ik heb van alles wel geprobeerd, inbox, postvak in, mijn email adres, locatie, maar zonder succes. Ik krijg steeds die foutmelding
 
En als je je e-mailadres invult?

Ik gebruik zelf ook geen outlook, dus ik kan het niet testen...
 
nogsteeds niet opgelost.

ik heb kunnen achterhalen dat mijn mailboxnaam "j.volker" is. Wanneer ik j.volker invoer krijg ik nogsteeds: "Mailbox for ‘j.volker’ not found".

weet iemand hier een oplossing voor?
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan