Question email crash!

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
Hi,
Trying to learn .net and facing an issue when calling the following proc if MSOutlook is not installed on the users machine (we have some liteclient PCs that don't) Hopefully this is a simple one that you experts can point me in the right direction of but the error is trying to load the interop even though i'm using Try... Catch...
Help!! :confused: (Edit: I should add that if MSOutlook is not installed then just getting the message that the user cannot send email is fine - I don't want them to ba able to)

VB.NET:
Imports Microsoft.Office.Interop

    Sub SendEmail(ByVal eSub$, ByVal Att$)
        Try
            Dim ol As New Outlook.Application
            Dim ns As Outlook.NameSpace
            Dim fdMail As Outlook.MAPIFolder
            Dim newMail As Outlook.MailItem
            '----------------------------------
            ns = ol.GetNamespace("MAPI")
            ns.Logon(, , True, True)
            fdMail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)
            newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
            With newMail
                .Subject = eSub
                .Body = "Please note that the attached document..."
                .Attachments.Add(Att)
                .SaveSentMessageFolder = fdMail
                .Display()
            End With
        Catch ex As Exception
            MsgBox("Unable to email", 48, c_AppName)
        End Try
    End Sub
 
Last edited:
maybe you can try checking the registry?

the error is inside the try block right? it's this line: Dim ol As New Outlook.Application that the error occurs on?

if it is, maybe you can just run a check on the registry key? something like:

Dim regKey As RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("Outlook.Application\CurVer")

if it is installed i believe it should have a key added. this way you can also check the version of outlook they have installed. for a 2010 version the value of the key is "Outlook.Application.14"
 
Thanks for the reply, much appreciated (the only one out of 66 views! I didn't think it would be that difficult but obviously it is)

As this is on a liteclient I don't have admin rights to check the registry. I ended up using the following code from a helpful forum which works well.

VB.NET:
    Function OutlookInstalled() As Boolean
        On Error Resume Next
        OutlookInstalled = (Not CreateObject("Outlook.application") Is Nothing)
    End Function
 
np, Oh cool. Thanks for sharing, didn't know about that method, im sure that'll come in handy :)
 
Back
Top