Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net Outlook Integration

Posted on 2014-03-05
5
Medium Priority
?
999 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Mailbox Overload?
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…
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: …

670 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