none
Open an existing Email with vb.net RRS feed

  • Question

  • I am struggling to open an existing email in Outlook from within a VB.Net application.  I am crashing on the GetItemFromID statement.  The error is Could not open the item. Try again.  Here is the code in am using:

    Thanks for any help

     Dim objOutlook As Outlook._Application
            Dim sClassComp = "IPM.Note"
            objOutlook = New Outlook.Application()
            Dim objNS As Outlook._NameSpace = objOutlook.Session
            Dim objFolder As Outlook.MAPIFolder = objNS.Folders.Item("Mailbox - DC Operations Job Quotes - 0018 HQ")
            objFolder = objFolder.Folders.Item("Inbox")
            Dim oItems As Outlook.Items = objFolder.Items
            Dim oMsg As Outlook.MailItem
     
            objFolder = objFolder.Folders.Item("Associates")
            objFolder = objFolder.Folders.Item(Trim(EmailfolderNameComboBox.Text))
            Dim strEmailID As String = Trim(EmailIDTextBox.Text)
     
     
            oMsg = objNS.GetItemFromID(strEmailID, objFolder.StoreID)
     
     
            oMsg.Display(True)
    • Moved by Mike Feng Tuesday, February 28, 2012 9:30 AM Outlook (From:Visual Basic Interop and Upgrade)
    Monday, February 27, 2012 2:57 PM

Answers

  • If this is automating Outlook from an external application you most likely need to do a MAPI logon. Instead of just getting Application.Session, use this and see if it helps:
     
            Dim sClassComp = "IPM.Note"
            objOutlook = New Outlook.Application()
            Dim objNS As Outlook._NameSpace = objOutlook.GetNameSpace("MAPI")
            objNS.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value)

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "chipwh" <=?utf-8?B?Y2hpcHdo?=> wrote in message news:20eea5a4-850b-4ef9-b3b6-443ef5698d86...

    I am struggling to open an existing email in Outlook from within a VB.Net application.  I am crashing on the GetItemFromID statement.  The error is Could not open the item. Try again.  Here is the code in am using:

    Thanks for any help

     Dim objOutlook As Outlook._Application
            Dim sClassComp = "IPM.Note"
            objOutlook = New Outlook.Application()
            Dim objNS As Outlook._NameSpace = objOutlook.Session
            Dim objFolder As Outlook.MAPIFolder = objNS.Folders.Item("Mailbox - DC Operations Job Quotes - 0018 HQ")
            objFolder = objFolder.Folders.Item("Inbox")
            Dim oItems As Outlook.Items = objFolder.Items
            Dim oMsg As Outlook.MailItem
     
            objFolder = objFolder.Folders.Item("Associates")
            objFolder = objFolder.Folders.Item(Trim(EmailfolderNameComboBox.Text))
            Dim strEmailID As String = Trim(EmailIDTextBox.Text)
     
     
            oMsg = objNS.GetItemFromID(strEmailID, objFolder.StoreID)
     
     
            oMsg.Display(True)

    Ken Slovak MVP - Outlook
    Tuesday, February 28, 2012 3:21 PM
    Moderator
  • Instantiation of outlook is in most part asynchronous - you have to wait untill everything is set up and connected - if you are using outlook with exchange you could check in loop with some wait for ExchangeConnectionMode on Session object to determine if outlook is ready for your next line of code.

    • Marked as answer by chipwh Wednesday, March 14, 2012 10:37 AM
    Tuesday, February 28, 2012 9:56 AM

All replies

  • Hi chipwh,

    Welcome to the MSDN Forum.

    I have moved this thread to OutLook forum for better support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 28, 2012 9:29 AM
  • Instantiation of outlook is in most part asynchronous - you have to wait untill everything is set up and connected - if you are using outlook with exchange you could check in loop with some wait for ExchangeConnectionMode on Session object to determine if outlook is ready for your next line of code.

    • Marked as answer by chipwh Wednesday, March 14, 2012 10:37 AM
    Tuesday, February 28, 2012 9:56 AM
  • If this is automating Outlook from an external application you most likely need to do a MAPI logon. Instead of just getting Application.Session, use this and see if it helps:
     
            Dim sClassComp = "IPM.Note"
            objOutlook = New Outlook.Application()
            Dim objNS As Outlook._NameSpace = objOutlook.GetNameSpace("MAPI")
            objNS.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value)

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "chipwh" <=?utf-8?B?Y2hpcHdo?=> wrote in message news:20eea5a4-850b-4ef9-b3b6-443ef5698d86...

    I am struggling to open an existing email in Outlook from within a VB.Net application.  I am crashing on the GetItemFromID statement.  The error is Could not open the item. Try again.  Here is the code in am using:

    Thanks for any help

     Dim objOutlook As Outlook._Application
            Dim sClassComp = "IPM.Note"
            objOutlook = New Outlook.Application()
            Dim objNS As Outlook._NameSpace = objOutlook.Session
            Dim objFolder As Outlook.MAPIFolder = objNS.Folders.Item("Mailbox - DC Operations Job Quotes - 0018 HQ")
            objFolder = objFolder.Folders.Item("Inbox")
            Dim oItems As Outlook.Items = objFolder.Items
            Dim oMsg As Outlook.MailItem
     
            objFolder = objFolder.Folders.Item("Associates")
            objFolder = objFolder.Folders.Item(Trim(EmailfolderNameComboBox.Text))
            Dim strEmailID As String = Trim(EmailIDTextBox.Text)
     
     
            oMsg = objNS.GetItemFromID(strEmailID, objFolder.StoreID)
     
     
            oMsg.Display(True)

    Ken Slovak MVP - Outlook
    Tuesday, February 28, 2012 3:21 PM
    Moderator