Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.Net Outlook Integration

Posted on 2014-03-05
5
Medium Priority
?
1,017 Views
Last Modified: 2014-03-06
I'm trying to create an app that will prompt the user for the Outlook Profile, then retrieve the message in the Inbox from the default Account.  Here is the code I have so far:

Imports System.Reflection
Imports Microsoft.Office.Interop

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()

        ' Get Mapi NameSpace.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
        oNS.Logon("Recruiter", Missing.Value, True, True)

        ' Get Messages collection of Inbox.
        Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim oItems As Outlook.Items = oInbox.Items
        Label1.Text = "Total : " & oItems.Count

        ' Get unread e-mail messages.
        'oItems = oItems.Restrict("[Unread] = true")
        Label2.Text = "Total Unread : " & oItems.Count

        ' Loop each unread message.
        Dim oMsg As Outlook.MailItem
        Dim i As Integer

        For i = 1 To oItems.Count
            oMsg = oItems.Item(i)

            DataGridView1.Rows.Add(i, oMsg.SenderName, oMsg.Subject, oMsg.ReceivedTime, oMsg.Body)
        Next

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oMsg = Nothing
    End Sub
End Class

Open in new window

I'm running this on my PC which has 3 Outlook Profiles: "Wade Chestnut" (default), "Recruiter" and "Archive".  The "Recruiter" is the one I want to be able to select, but it doesn't prompt for a Profile and Inbox messages are pulled from my Account .  I do receive the warning prompt from Outlook 2010 about a program trying to access e-mail addresses but never the prompt for the Profile even though I have the 3rd Logon parameter set to True which is "Show Dialog as Object".
0
Comment
Question by:wchestnut
  • 3
  • 2
5 Comments
 

Author Comment

by:wchestnut
ID: 39906654
I also tried this but it didn't make a difference:
oNS.Logon(Nothing, Nothing, True, False)

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39906708
Do you want the dialog? If you just specify the name in Logon method, it should connect to that profile automatically.
0
 

Author Comment

by:wchestnut
ID: 39908039
Yes, I actually need the Profile selection prompt so that the program can be used on other computers with different Outlook Profile names.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39908984
Following is from MSDN

Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.
If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.
If Outlook is not running and you only want to start Outlook with the default profile, do not use the Logon method. A better alternative is shown in the following code example, InitializeMAPI: first, instantiate the Outlook Application object, then reference a default folder such as the Inbox. This has the side effect of initializing MAPI to use the default profile and to make the object model fully functional.


http://msdn.microsoft.com/en-us/library/office/ff861594.aspx


So try running your code while Outlook is not running.
0
 

Author Closing Comment

by:wchestnut
ID: 39909591
Thanks, CodeCruiser.  This make some sense.

I tried running it a couple more times with Outlook closed, selecting Profiles but ran into some issues then said screw it.  The users aren't going to want to close Outlook just to run this app and it should feed off of the one that's already open.  I'm able to re-run it with my "Recruiter" Profile open and it read the Inbox which is my goal.

Thanks, again!
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
Today as you open your Outlook, you witness an error message: “Outlook is using an old copy of your Outlook Data File…”. Probably, Outlook is accessing an old OST file.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question