none
Problem attaching an excel-sheet to an Outlook.MailItem.HTMLBody RRS feed

  • General discussion

  • I'm trying to read and attach an Excel-file to an HTMLBody of an Outlook.MailItem but I cannot figure out how to convert a string to hmtl. Anyone with a solution? This is my code so far:

    sHTML =

    File

    .ReadAllText(sPath)

    .BodyFormat = Outlook.

    OlBodyFormat

    .olFormatHTML

    .HTMLBody = sHTML


    ElreyBello
    Thursday, January 19, 2012 9:54 AM

All replies

  • The easiest way to see what Outlook would do is to do what you want in the user interface and then grab HTMLBody to see what it looks like. You can insert an Excel file in an HTML email. Then you can either use some Outlook VBA code to output HTMLBody to the Immediate window or you can use a toll such as OutlookSpy (www.dimastr.com/outspy) to get the HTMLBody property and copy it to a Notepad file for examination.
     
    There will be a lot of extra stuff not actually needed for HTML, for your purposes you can concentrate on the text between the <body> and </body> tags, as that's where the "meat" is.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ElreyBello" <=?utf-8?B?RWxyZXlCZWxsbw==?=> wrote in message news:e2ec5ace-b1e9-4ae5-a053-c5cbb6c402f7...

    I'm trying to read and attach an Excel-file to an HTMLBody of an Outlook.MailItem but I cannot figure out how to convert a string to hmtl. Anyone with a solution? This is my code so far:

    sHTML =

    File

    .ReadAllText(sPath)

    .BodyFormat = Outlook.

    OlBodyFormat

    .olFormatHTML

    .HTMLBody = sHTML


    ElreyBello

    Ken Slovak MVP - Outlook
    Thursday, January 19, 2012 3:09 PM
  • The problem is that when I paste the excel to the HTMLBody it is printed unformatted and not being able to edit as a normal excel-sheet. It seems as if I read the excel directly to the string it does not contain the formatting. When I did this in VBA I had the following code and it worked fine, I don't know what is the equivalent in VB.Net:

    Set fsReport = New Scripting.FileSystemObject
    Set tsReport = fsReport.OpenTextFile(sPath, ForReading)
    sHTML = tsReport.ReadAll
    .HTMLBody = sHTML


    ElreyBello
    Thursday, January 19, 2012 3:36 PM
  • The equivalent would probably be something where you started a StreamReader with the file path and read the entire file as a string, then set HTMLBody:
     

    Dim sr As New StreamReader(sPath)

    Dim sFile As String = sr.ReadToEnd()

    "sFile" then contains the contents of the file as a string.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ElreyBello" <=?utf-8?B?RWxyZXlCZWxsbw==?=> wrote in message news:f417c0ce-acf4-460f-84e4-bda5653cd223...

    The problem is that when I paste the excel to the HTMLBody it is printed unformatted and not being able to edit as a normal excel-sheet. It seems as if I read the excel directly to the string it does not contain the formatting. When I did this in VBA I had the following code and it worked fine, I don't know what is the equivalent in VB.Net:

    Set fsReport = New Scripting.FileSystemObject
    Set tsReport = fsReport.OpenTextFile(sPath, ForReading)
    sHTML = tsReport.ReadAll
    HTMLBody = sHTML


    ElreyBello

    Ken Slovak MVP - Outlook
    Thursday, January 19, 2012 3:53 PM
  • Thanks, I'll try this and report back to you.
    ElreyBello
    Friday, January 20, 2012 10:37 AM