Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a bit stuck.
There is a need for a simple one click program to pick up data on an Excel spreadsheet and dump it into the body of an email then send it.

I am using the code below to create the email but as you can see it is set up for Outlook.
Is this also workable for thunderbird or are they incompatible?

It seems to work but it ain't sending!

(BTW - sorry about the GOTOs - I am not proud!)



VB
Imports Excel = Microsoft.Office.Interop.Excel
Imports Olook = Microsoft.Office.Interop.Outlook

Public Class Form1
    
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim xlRange As Excel.Range

   
    Dim olApp As New Olook.Application
    Dim olMail As Olook.MailItem

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        xlWorkBook = xlApp.Workbooks.Open("C:\TESTWORKBOOK.xlsx")
        xlWorkSheet = xlWorkBook.Sheets("Sheet1")
        xlRange = xlWorkSheet.Range("A1:F20")
        olMail = olApp.CreateItem(0)

        On Error Resume Next
        With olMail
            .To = "joe.bloggs@someaddress.com"
            .CC = ""
            .BCC = ""
            .Subject = "This is a test"
            .HTMLBody = RangetoHTML(xlRange)
            .Display()
        End With
        On Error GoTo 0

        xlWorkBook.Close (False)
        xlApp.Quit()
        releaseObject (xlApp)
        releaseObject (xlWorkBook)

    End Sub

    Function RangetoHTML(rng As Excel.Range)

        Dim fso As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Excel.Workbook

        TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

        rng.Copy()

        TempWB = xlApp.Workbooks.Add(1)

        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial(Paste:=8)
            .Cells(1).PasteSpecial(-4163, , False, False)
            .Cells(1).PasteSpecial(-4122, , False, False)
            .Cells(1).Select()
            xlApp.CutCopyMode = False
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete()
            On Error GoTo 0
        End With

        With TempWB.PublishObjects.Add( _
             SourceType:=4, _
             Filename:=TempFile, _
             Sheet:=TempWB.Sheets(1).Name, _
             Source:=TempWB.Sheets(1).UsedRange.Address, _
             HtmlType:=0)
            .Publish (True)
        End With

        fso = CreateObject("Scripting.FileSystemObject")
        ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
        RangetoHTML = ts.ReadAll
        ts.Close()
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                              "align=left x:publishsource=")
        TempWB.Close(savechanges:=False)

        Kill (TempFile)

        ts = Nothing
        fso = Nothing
        TempWB = Nothing
    End Function

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class
Posted
Comments
Abinash_Sahoo 6-Apr-14 18:36pm    
Instead of using outlook interop, did you try using SmtpClient Class (http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx)?

1 solution

For Thunderbird you could use the Shell function.

Here is an example I found that adds text to the body and attaches a file.
Shell ("C:\Program Files (x86)\Mozilla Thunderbird\Thunderbird.exe -compose to='mymailname@gmail.com',subject='Test message from thunderbird',preselectid='id1',body='Example message from myText.txt',attachment='file:///c:\onwin.png'")

This website provides the command line arguments for Thunderbird: http://kb.mozillazine.org/Command_line_arguments_(Thunderbird)[^]

Good luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900