Solved

VB.net Excel Add-In Sending Outlook Email when Outlook is closed

Posted on 2013-01-17
7
823 Views
Last Modified: 2013-01-21
Hi

I am using the following code in my VB.net Excel add-in to send an email. It works just fine
if Outlook is open but crashes when Outlook is closed. How can I modify the code to send it even if Outlook is close?

Imports Microsoft.Office.Interop.Outlook

Module modEmail

    Public Sub oSendEmail(ByVal oTo As String, ByVal oCc As String, ByVal oSubject As String, ByVal oBody As String, ByVal oAttachment As String)

        Try


            ' Create an Outlook application.
            Dim oOutApp As Microsoft.Office.Interop.Outlook.Application
            oOutApp = New Microsoft.Office.Interop.Outlook.Application

            ' Create a new MailItem.
            Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem
            oMsg = oOutApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)

            oMsg.Subject = oSubject
            oMsg.Body = oBody

            ' TODO: Replace with a valid e-mail address.
            oMsg.To = oTo
            oMsg.CC = oCc

            ' Add an attachment
            Dim sSource As String = oAttachment 'eg "C:\Temp\Hello.txt"

            'Dim sBodyLen As String = oMsg.Body.Length
            Dim sBodyLen As Integer = oBody.Length


            Dim oAttachs As Microsoft.Office.Interop.Outlook.Attachments
            Dim oAttach As Microsoft.Office.Interop.Outlook.Attachment
            'only attach if there is something there
            If oAttachment <> "" And oAttachment <> Nothing Then
                oAttachs = oMsg.Attachments
                If oAttachment <> Nothing And oAttachment <> "" And System.IO.File.Exists(oAttachment) = True Then
                    oAttach = oAttachs.Add(sSource, , sBodyLen + 1)
                    'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
                End If
            End If

            ' Send
            oMsg.Send()
            ' Clean up
            oOutApp = Nothing
            oMsg = Nothing
            oAttach = Nothing
            oAttachs = Nothing
        Catch
            MsgBox("An error occurred in trying to send an email! " & Err.Description)
        End Try

    End Sub

End Module
0
Comment
Question by:murbro
  • 4
  • 3
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38789603
This code should work given that you have

oOutApp = New Microsoft.Office.Interop.Outlook.Application

What is the error you get?
0
 

Author Comment

by:murbro
ID: 38791866
Hi. I included oOutApp = New Microsoft.Office.Interop.Outlook.Application
but still get the error as shown in the attached image
1
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38792277
Do you know which line is generating the error? Does outlook start? Before running this account when outlook is closed, is the outlook.exe in processes list in task manager?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:murbro
ID: 38800668
Hi

The error occurs at the line  oMsg.Send.
Nothing visible happens. Outlook doesn't start.
outlook.exe is not  in processes list in task manager

Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38801041
Well the

oOutApp = New Microsoft.Office.Interop.Outlook.Application

line should start Outlook.

See if this trick works for you

http://stackoverflow.com/questions/11330101/can-only-send-email-via-outlook-if-outlook-is-open
0
 

Author Closing Comment

by:murbro
ID: 38801255
Thanks very much
0
 

Author Comment

by:murbro
ID: 38801428
Here's the working code for anyone that views this:

    Public Sub oSendEmail(ByVal oTo As String, ByVal oCc As String, ByVal oSubject As String, ByVal oBody As String, ByVal oAttachment As String)

        Try


            ' Create an Outlook application.
            Dim oOutApp As New Microsoft.Office.Interop.Outlook.Application
            oOutApp = New Microsoft.Office.Interop.Outlook.Application

            Dim ns As Microsoft.Office.Interop.Outlook.NameSpace = oOutApp.GetNamespace("MAPI")

            Dim f As Microsoft.Office.Interop.Outlook.MAPIFolder
            f = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox)

            System.Threading.Thread.Sleep(3000) 'a bit of startup grace time.



            ' Create a new MailItem.
            Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem
            oMsg = oOutApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)

            oMsg.Subject = oSubject
            oMsg.Body = oBody

            ' TODO: Replace with a valid e-mail address.
            oMsg.To = oTo
            oMsg.CC = oCc

            ' Add an attachment
            Dim sSource As String = oAttachment 'eg "C:\Temp\Hello.txt"

            'Dim sBodyLen As String = oMsg.Body.Length
            Dim sBodyLen As Integer = oBody.Length


            Dim oAttachs As Microsoft.Office.Interop.Outlook.Attachments
            Dim oAttach As Microsoft.Office.Interop.Outlook.Attachment
            'only attach if there is something there
            If oAttachment <> "" And oAttachment <> Nothing Then
                oAttachs = oMsg.Attachments
                If oAttachment <> Nothing And oAttachment <> "" And System.IO.File.Exists(oAttachment) = True Then
                    oAttach = oAttachs.Add(sSource, , sBodyLen + 1)
                    'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
                End If
            End If

            ' Send
            oMsg.Send()
            ' Clean up
            oOutApp = Nothing
            oMsg = Nothing
            oAttach = Nothing
            oAttachs = Nothing
        Catch
            MsgBox("An error occurred in trying to send an email! " & Err.Description)
        End Try

    End Sub
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now