Solved

VB.Net Outlook Integration

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

If you don't know how to downgrade, my instructions below should be helpful.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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