locked
setting font size when creating HTML text in a stringbuilder RRS feed

  • Question

  • Hi

    I am creating a HTMLBody for an email using stringbuilder

    All working fine for setting font family, bold etc, but cannot seem to set the font size to 11pt

    Any help please

    This is part of my code:-

    Dim sb As New System.Text.StringBuilder
            sb.AppendLine("<font face='calibri'><font size:11.0pt>")
            sb.AppendLine("Hi<p>")


    Darren Rose

    Sunday, October 19, 2014 11:25 AM

Answers

All replies

  • Try this:

    sb.AppendLine("<span style='font-family: Calibri; font-size: 10pt;'>Hi</span>");


    And maybe use <p> or <div> instead of <span>.


    • Edited by Viorel_MVP Sunday, October 19, 2014 11:53 AM
    Sunday, October 19, 2014 11:51 AM
  • Hi

    That works to set font and size for "Hi" but the font size/type changes back for rest of my lines

    Should have mentioned I am adding multiple lines using sb.appendline and I need the font family/size to stick for all of the lines


    Darren Rose

    Sunday, October 19, 2014 11:57 AM
  • Then do not end the string with “</span>” right now. Put “</span>” at the end of the message.


    Sunday, October 19, 2014 12:00 PM
  • Then do not end the string with “</span>” right now. Put “</span>” at the end of the message.


    excellent - thank you very much

    Can I create a hyperlink in my stringbuilder just as easily?


    Darren Rose

    Sunday, October 19, 2014 12:04 PM
  • For links use <a> element: http://msdn.microsoft.com/en-us/library/ie/ms535173(v=vs.85).aspx. Based on example, try this:

    sb.AppendLine("<a href=""http://www.microsoft.com"">Microsoft home page.</a>")

    You can find there other HTML elements and attributes. For various properties that can be specified inside the style=”…” attribute see http://msdn.microsoft.com/en-us/library/ie/hh869403(v=vs.85).aspx. Probably not all of the features are supported by mail programs.

    Sunday, October 19, 2014 12:17 PM
  • Thank you Viorel - you have been very helpful!!!

    Darren Rose

    Sunday, October 19, 2014 12:34 PM
  • Hello,

    Did you know that you can create html elements for email using xml literals that include css?

    The following shows a css section that is used in the body. I am using a web browser controls to show the outcome but this will work in sending messages.

    Dim dt As DataTable = New DataTable("Services")
    dt.Columns.Add("Identifier", GetType(Integer))
    dt.Columns.Add("FirstName", GetType(String))
    dt.Columns.Add("LastName", GetType(String))
    
    
    dt.Columns("Identifier").AutoIncrement = True
    dt.Columns("Identifier").AllowDBNull = False
    dt.Columns("Identifier").ReadOnly = True
    dt.Columns("Identifier").AutoIncrementSeed = 1
    
    dt.Rows.Add(New Object() {Nothing, "Kevin", "Gallagher"})
    dt.Rows.Add(New Object() {Nothing, "Mary", "Jones"})
    dt.Rows.Add(New Object() {Nothing, "Bill", "Smith"})
    dt.Rows.Add(New Object() {Nothing, "Karen", "Starr"})
    
    Dim MessageBody = _
            <html>
                <style>
                TD {background-color: green;color: #F0F8FF;padding-right:15px;}
                .THeader {background-color: Yellow;color: Black;}
                .BoldAndLarge {font-weight: bold;font-size:21.0pt}
                </style>
                <body>
                    <p>This is <span class='BoldAndLarge'>how</span> it is done.</p>
                    <table border="0">
                        <tr>
                            <td class='THeader'>ID</td><td class='THeader'>First</td><td class='THeader'>Last</td>
                        </tr>
                        <%= From T In dt.AsEnumerable _
                            Select _
                            <tr>
                                <td width="80px">
                                    <%= T.Field(Of Integer)("Identifier") %>
                                </td>
                                <td width="45px">
                                    <%= T.Field(Of String)("FirstName") %>
                                </td>
                                <td width="30px">
                                    <%= T.Field(Of String)("LastName") %>
                                </td>
                            </tr> %>
                    </table>
                </body>
            </html>
    
    WebBrowser1.DocumentText = MessageBody.ToString
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, October 19, 2014 2:38 PM
  • Thanks Kevin

    No I didn't know it could be done like that

    Thanks for letting me know, I will give it a try


    Darren Rose

    Sunday, October 19, 2014 2:52 PM
  • Thanks Kevin

    No I didn't know it could be done like that

    Thanks for letting me know, I will give it a try


    Darren Rose

    Also this link W3Schools provides pretty good examples which you can alter and test online.

    In either of the below links from the above link you can select "Try it yourself". And while trying it you can alter anything in it in the left window where the code is and select Submit which will display in the right window what you now are using.

    HTML Styles

    CSS Font


    La vida loca

    Sunday, October 19, 2014 5:08 PM
  • Thanks Mr. Monkeyboy - very useful links!!!

    Darren Rose

    Sunday, October 19, 2014 5:11 PM
  • something like this?

            Dim sb As New System.Text.StringBuilder
            sb.AppendLine("<!DOCTYPE html>")
            sb.AppendLine("<html itemscope="""" itemtype=""http://schema.org/WebPage"" lang=""en"">")
            sb.AppendLine("    <body>")
            Dim fontFamilies As String() = {"consolas", "arial", "tahoma", "verdana"}
            Dim htmlLine As String = "        <font Family='{0}' Size='{1}'>{2}</font></br>"
            For Each Font As String In fontFamilies
                sb.AppendLine(String.Format(htmlLine, Font, 8, "HTML Line"))
            Next
            sb.AppendLine("    </body>")
            sb.AppendLine("</html>")
            MsgBox(sb.ToString)


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Sunday, October 19, 2014 6:35 PM
  • Darren,

    If you're using your Dev controls, don't forget that with a RichTextEdit, you can directly export to an HTML file.


    Still lost in code, just at a little higher level.

    :-)

    Sunday, October 19, 2014 6:55 PM
  • Darren,

    If you're using your Dev controls, don't forget that with a RichTextEdit, you can directly export to an HTML file.


    Still lost in code, just at a little higher level.

    :-)

    Hi Frank

    Thanks for reply - no as this is "hopefully" going to end up as an outlook add-in I had kept away from using dev tools, but good point, had forgotten about that

    Hope you are well


    Darren Rose

    Sunday, October 19, 2014 6:57 PM
  • Hi Frank

    Thanks for reply - no as this is "hopefully" going to end up as an outlook add-in I had kept away from using dev tools, but good point, had forgotten about that

    Hope you are well


    Darren Rose

    It was a thought. :)

    Still lost in code, just at a little higher level.

    :-)

    Sunday, October 19, 2014 6:59 PM