Solved

VB.Net Outlook Integration

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10  with Outlook  2016 - Turn off Windows Security pop up 8 58
Need help with another query 10 39
exchange 13 25
EXCHANGE, OUTLOOK, CALENDAR 12 41
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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…

726 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