How to SaveHTMLRange to a MemoryStream

Posted by: mikedempsey on 8 September 2017, 4:34 am EST

  • Posted 8 September 2017, 4:34 am EST

    SaveHTMLRange() allows me to specify a Stream instead of a File but I can not figure out how to use the Stream after it is populated.

    I pass in a new MemoryStream (I tried both with and without specifying an initial capacity) and the function returns successfully.

    The problem is that the Stream is already closed when it returns.

    If I examine the Stream.GetBuffer() contents it appears to be populated, but there is no 'Open' method that would allow me to re-open and use it.

    All I am trying to do here is to copy the selected cells to the clipboard in HTML format.
    If I save it to a temp file and re-read the file it works fine, but I would prefer to use a memory object to avoid the file I/O.

    (In VB6 I could simply have treated Stream.GetBuffer() as a string ... but that is not allowed in .Net.)

    Any ideas here?



  • Replied 8 September 2017, 4:34 am EST

    I am trying to add a 'Copy as HTML' function. (Copy to the clipboard that is)

    Immediately after writing the range to a memory stream I need to send it to the clipboard using the HTML clipboard format.
    (Using Clipboard.SetDate())

    The user who requested the function will them paste it into another applicatin that is expecting to receive an HTML table. (Most likely an email message that is using the HTML option)

    As I said, it works fine if I export to a file and then simply read the entire file back into a string and write that string to the clipboard.
    I just wanted to avoid the IO overhead.



  • Replied 8 September 2017, 4:34 am EST


    You should be able to use the GetBuffer method to get the byte array to store in the Clipboard. I can not find the clibard format to use to have this paste as HTML code in email.

  • Replied 8 September 2017, 4:34 am EST

    OK the following works.
    The part that I didn't know was the use of Text.Encoding

    <FONT color=#008000 size=2><FONT color=#008000 size=2>

    <FONT color=#0000cc>Dim asc As System.Text.Encoding = System.Text.Encoding.ASCII
    </FONT></FONT></FONT><FONT color=#0000cc><FONT size=2><FONT size=2>Dim tStr As String = asc.GetString(ms.GetBuffer())
    </FONT></FONT><FONT size=2><FONT size=2>Clipboard.SetData(DataFormats.Html, tStr.Substring(3))</FONT></FONT></FONT>

    <FONT color=#0000cc><FONT size=2><FONT size=2><FONT color=#000000>The substring is required because the buffer appears to have 3 strange bytes (display as '???') at the start.</FONT></FONT></FONT></FONT><FONT color=#0000cc><FONT size=2><FONT size=2><FONT color=#000000>
    </FONT><FONT color=#000000>I tried using UTF8 encoding instead but that still has one strange byte (displays as '?')</FONT></FONT></FONT></FONT>

    <FONT size=2><FONT color=#000000 size=2>So 2 questions </FONT></FONT>

    <FONT size=2><FONT color=#000000 size=2>1) What is the actual encoding of the byte array that you return (it looks like probably UTF8)</FONT></FONT>

    <FONT size=2><FONT color=#000000 size=2>2) What is this extra byte at the start?</FONT></FONT>

    <FONT color=#0000cc><FONT size=2><FONT size=2>Thanks

  • Replied 8 September 2017, 4:34 am EST


    1) We do use UTF8 encoding for this method writing to a stream.

    2) The extra byte is the type of encoding that is used on the stream.

  • Replied 8 September 2017, 4:34 am EST

    Mike -

    You say you are wanting to re-open and use it but you don't specify how or where you are wanting to do this.  It is saved as an HTML table so I would assume you would want it in an html page since that is the purpose of the method.

  • Replied 8 September 2017, 4:34 am EST


    I guess Microsoft's UTF8 Encoding class just isnt very good then.

    UTF16 has a 2 character BOM prefix, but when converted to UTF8 it becomes 3 bytes.
    When I read from the UTF8 Encoding it automatically strips off the first 2 bytes ... but it should be stripping off all 3.

    I'll just ignore the first byte.


Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels