Solved

VB.Net Outlook Integration

Posted on 2014-03-05
5
892 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now