Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VB.net Add-in Error sending Outlook email

Posted on 2013-01-03
2
Medium Priority
?
484 Views
Last Modified: 2013-01-05
Hi

I am using the following code to send an email via Outlook from my Excel VB.net add-in.
I get an error at the line marked ERROR HERE. The error is "Process aborted...."
What am I doing wrong?

Imports Microsoft.Office.Interop.Outlook

Module modEmail

    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
            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 ''ERROR HERE
            Dim oAttachs As Microsoft.Office.Interop.Outlook.Attachments = oMsg.Attachments
            Dim oAttach As Microsoft.Office.Interop.Outlook.Attachment
            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

            ' 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
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38740704
Change

Dim sBodyLen As String = oMsg.Body.Length

to

Dim sBodyLen As Integer= OBody.Length
0
 

Author Closing Comment

by:Murray Brown
ID: 38746351
Thanks very much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

705 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