none
Trouble Late Binding Outlook Email from VB.NET Application RRS feed

  • Question

  • Hi All,

    I have an issue late binding outlook from my VB.NET app. Code is as follows

                Dim outlook As Object = Nothing
                Dim email As Object = Nothing
                Try
                    ' Create OutlookAdapter instance
                    outlook = CreateObject("Outlook.Application")
                    ' Create the mail message
                    email = outlook.CreateItem(MAIL_MESSAGE)
                    email.To = GetContactsString([to])
                    email.CC = GetContactsString(cc)
                    email.BCC = GetContactsString(bcc)
                    email.Subject = subject
                    email.Body = message
                    ' Add attachments
                    Dim count As Integer = 1
                    For Each attachment As EmailAttachment In attachments
                        email.Attachments.Add(attachment.File.FullName, Type.Missing, Type.Missing, attachment.Title)
                        count += 1
                    Next
                    email.Display(False)
                Catch ex As Exception
                Finally
                    email = Nothing
                    outlook = Nothing
                End Try
            End Sub
    

    NOTE: MAIL_MESSAGE is an integer constant of value 0

    As long as the program is run from a machine which has Outlook installed and the user logged in has a valid mail profile then when Outlook is open and this code is run an email message is displayed on screen with the correct details.

     

    If Outlook is closed and the code is run you can see that Outlook application is created if you look in task manager, but the email is never displayed.

    In the task bar I get an Outlook icon with a cog and a message that says "Another program is using Outlook. To disconnect programs exit outlook, click the outlook icon and click Exit now."

    Any ideas?

    • Moved by Mark Liu-lxfModerator Monday, December 26, 2011 5:43 AM Outlook 2010 (From:Visual Basic General)
    Friday, December 23, 2011 11:08 AM

All replies

  • One more thing - This is Outlook 2010
    Friday, December 23, 2011 12:53 PM
  • Try setting the Visible property of the Outlook Application object to True.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, December 23, 2011 1:13 PM
  • I added this line of code after creating the outlook Object.

    outlook.Visible = True
    

    I then received a MissingMember Exception

     

    Public member 'Visible' on type 'ApplicationClass' not found.

    Friday, December 23, 2011 3:16 PM
  • Yeah, I forgot that Outlook operates a bit differently than the other Office apps. See if the below code helps:

            Dim outlook As Object = Nothing
            Dim outlookNamespace As Object
            Dim outlookExplorer As Object
            Dim outlookFolder As Object
    
            outlook = CreateObject("Outlook.Application")
            outlookNamespace = outlook.GetNamespace("MAPI")
            outlookFolder = outlookNamespace.GetDefaultFolder(6) 'Inbox
            outlookExplorer = outlook.Explorers.Add(outlookFolder, 0) 'Display folder normal
            outlookExplorer.Activate()
    
    

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, December 23, 2011 3:55 PM
  • Hi Subtle81,

    Welcome to the MSDN forum.

    This queue is about Visual Basic. I’m afraid that your topic about Outlook 2010 is unsuitable here. Outlook for Developers forum is a special forum to deal with the Outlook issue. For better support, I will move this thread to Outlook for Developers forum.

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 26, 2011 5:42 AM
    Moderator
  • Hi Subtle81,

     

    Thanks for posting in the MSDN Forum.

    It's based on my experience that  you have error in the For each circle. There has some unsolved variables in that circle. Would you please explain those variables? If you omit those variables I think you snippet will work fine.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 30, 2011 12:00 PM
    Moderator
  • Hi,

    To clear up any confusion the full method and signature and constant declaration is as follows:

    EmailAttachment is a class to model an attachment and contains properties to access the FileInfo of the file to be attached, the name of the file to be attached

    Private Const MAIL_MESSAGE As Integer = 0        
    
    Public Shared Sub OpenEmail(ByVal [to] As IList(Of String), ByVal cc As IList(Of String), ByVal bcc As IList(Of String), _
                                        ByVal subject As String, ByVal message As String, ByVal attachments As IList(Of EmailAttachment))
                Dim outlook As Object = Nothing
                Dim email As Object = Nothing
                Try
                    ' Create OutlookAdapter instance
                    outlook = CreateObject("Outlook.Application")
                    ' Create the mail message
                    email = outlook.CreateItem(MAIL_MESSAGE)
                    email.To = GetContactsString([to])
                    email.CC = GetContactsString(cc)
                    email.BCC = GetContactsString(bcc)
                    email.Subject = subject
                    email.Body = message
                    ' Add attachments
                    Dim count As Integer = 1
                    For Each attachment As EmailAttachment In attachments
                        email.Attachments.Add(attachment.File.FullName, Type.Missing, Type.Missing, attachment.Title)
                        count += 1
                    Next
                    email.Display(False)
                Catch ex As Exception
                Finally
                    email = Nothing
                    outlook = Nothing
                End Try
            End Sub
    

     

    Friday, December 30, 2011 2:57 PM
  • Hi Paul,

    This kind of works in that it will open outlook if it is closed and then display the email message that is created subsequently. However, if Outlook is already open then a new instance is created so the user ends up with multiple instances open.

    As this application can be published as a Citrix application then many users can be executing the application from the same terminal so it's not possible just to check if Outlook is already open as it could be another users instance of outlook.

    If I can avoid checking to see if there is an instance of outlook open under the user's account then I would as that seems like a lot of overhead for what is supposed to be a simple case of adding a file to an email.

    I don't understand why the email isn't displayed when outlook is originally closed even though you can clearly see that an instance of outlook has been created in task manager. So I assume the email has also been created (as I step over the code that does this).

    Is there a way to display the email without having to call activate on the outlookexplorer/main explorer? All I really want to see is the email, with the subject, body and attachment so the user can enter the recipient.

     

    Regards

    Friday, December 30, 2011 6:59 PM