שתף באמצעות


Using VB and Outlook email

Question

Thursday, January 17, 2019 12:48 PM

I recently got a new PC and had to download a new Visual Studio/Visual Basic.  The new version is 15.9.4. The version prior this, I am not sure.  Anyway I had to make changes to a program that sends an email via Outlook.  This routine worked fine in older versions of Visual Basic.  Now when I make changes using the new VB  the following statement gives an error:

Dim MSO As New Microsoft.Office.Interop.Outlook.Application

I researched the error and found that if you stop out from running on you PC it works but if Outlook is started it does not.  The users of this program will not accept this as they will not want to stop outlook to run the application.    Any ideas as to why this is happening?   Thanks in advanced.

SC

All replies (9)

Thursday, January 17, 2019 1:37 PM

Please show full code if you want assistance. Normally I've seen the reverse, code works with Outlook open and fails if not open.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Thursday, January 17, 2019 1:43 PM

  ' Obviously, each element in the procs() array will contain an outlook process

        Dim MSO As New Microsoft.Office.Interop.Outlook.Application
        'Create a new Message object
        Dim msg As Microsoft.Office.Interop.Outlook.MailItem

        'Compose the Message
        msg = MSO.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
        msg.Subject = gvEmailSubject
        msg.To = gvEmailTo
        msg.BCC = gvEmailBCC
        '***ADD AN ATTACHMENT***
        'This parameter just has to point to a valid file
        ' msg.Attachments.Add("C:\Users\ & gUserName & "\desktop\temp.pdf")
        '*******************************
        'If you have SendOnBehalf permissions, you can specify a SOB user
        'msg.SentOnBehalfOfName = "Some Other User"
        ''Create an HTML formatted body
        Dim body As String
        'body = "<h4>This is an HTML Email</h4><p>Hello, here is a test message.</p>"
        body = gvEmailMsg
        ''Set the body format and contents
        msg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
        msg.HTMLBody = body

        'Send the message

        msg.Send()

SC


Thursday, January 17, 2019 2:54 PM

I just tested this with Outlook open and it works as expected. Also with Outlook closed.

Option Strict On
Option Infer On

Imports System.IO
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SendMailDemo()
    End Sub
    Public Sub SendMailDemo()
        Try
            ' Create the Outlook application.
            Dim outlookApp As New Outlook.Application()
            ' Create a new mail item.
            Dim outlookMailItem = DirectCast(outlookApp.CreateItem(OlItemType.olMailItem), MailItem)

            outlookMailItem.HTMLBody = "Hello, Jawed your message body will go here!!"
            'Add an attachment.
            Dim displayNameForAttachment = "MyAttachment"
            Dim position As Integer = CInt(outlookMailItem.Body.Length) + 1
            Dim attachType = CInt(OlAttachmentType.olByValue)

            Dim wordDocPathName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Document.docx")
            Dim theAttachment As Attachment = outlookMailItem.Attachments.Add(
                wordDocPathName, attachType, position, displayNameForAttachment)

            outlookMailItem.Subject = "Your Subject."
            Dim recipients As Recipients = outlookMailItem.Recipients
            ' Change the recipient in the next line if necessary.
            Dim oRecip = recipients.Add("TODO")
            oRecip.Resolve()
            outlookMailItem.Send()
            ' Clean up.
            oRecip = Nothing
            recipients = Nothing
            outlookMailItem = Nothing
            outlookApp = Nothing
            'end of try block
        Catch ex As System.Exception
            Console.WriteLine("Put breakpoint here")
        End Try
    End Sub
End Class

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Thursday, January 17, 2019 3:38 PM

I took you code and copied in exactly as you have it. When I run it the  following cause the Catch to trigger

  ' Create the Outlook application.
            Dim outlookApp As New Outlook.Application()

and get the following:

System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at WindowsApp2.Form1.SendMailDemo() in C:\Users\sclark\source\repos\WindowsApp2\WindowsApp2\Form1.vb:line 146
The program '[29596] WindowsApp2.exe' has exited with code -1 (0xffffffff).

SC


Thursday, January 17, 2019 3:49 PM

Here is my project.

https://1drv.ms/u/s!AtGAgKKpqdWjjH7wEpR_oH8_SM5i

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Thursday, January 17, 2019 4:00 PM

I download your example and I get the same error.  

SC


Thursday, January 17, 2019 4:05 PM

I download your example and I get the same error.  

SC

Only other thing I can think of is the version of the Outlook reference could be an issue.

This is what I'm using, if you have a different version methods and or properties may be different or that you compiled 64 bit version 32 bit.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Thursday, January 17, 2019 4:10 PM

I am required to use Office 365 if this makes a difference.

SC


Thursday, January 17, 2019 5:17 PM

I am required to use Office 365 if this makes a difference.

SC

You need then to work with the following NuGet packages which is much more to work with when working with Office 365 as these are suited more to web solutions and that is the flavor of the current time.

 

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator