Solved

VB.net Excel Add-in Allowing user to change something in an email before send

Posted on 2013-01-22
2
244 Views
Last Modified: 2013-01-22
Hi

I use the following code in my VB.net Excel add-in to send an Outlook email without the user  even seeing it. How do I update the code to make Outlook visible and allow the user to hit the send button?


    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
            If Form_Supplier_Payments.chkCheckEmail.Checked = True Then
                'New code here
            Else
                oMsg.Send()
            End If

            ' 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:murbro
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
Change

 oMsg.Send()

to

 oMsg.Display()

and remove the cleanup code.
0
 

Author Closing Comment

by:murbro
Comment Utility
Once again, great answer. Thanks very much
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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…

771 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

8 Experts available now in Live!

Get 1:1 Help Now