Copying Visual Studio Code Snippets to the Clipboard as HTML

How can I fix it?

You have to use the “Add Reference” menu. Add references to System.Drawing and System.Web.

Hey Jeff,

Just tried this out, and there seems to be a problem with bold text in the code. I have VS.NET setup to use Bold for types and they all get mangled:

Here’s an example (picked out of FreeTextBox):

    static {\b WebStoreConfig} _Configuration;

    // *** Some worker values
    public static {\b DateTime} EmptyDate = {\b Convert}{\b .ToDateTime(}"01/02/1900");
    public static {\b DateTime} DateCompare = {\b Convert}{\b .ToDateTime(}"01/03/1900");

Looks like this should be easy to fix though - I’m too tired tonight to deal with it here though :-}

Good catch Rick “Mr. Bold” Strahl.

Clearly I never tested that… so, here’s the fix. Add this at line 310 of the macro:

'-- map bold
rtf = Regex.Replace(rtf, “{\b\s([^}]+?)}”, “b$1/b”)

I’ll upload an updated version tomorrow.

Thanks Jeff,

That fixes things - well from your end :-}. It looks like VS.NET is not generating proper RTF from the code, because the bolding is incorrect in many places - or at least it doesn’t match what I see in the code editor. I see the same thing if I paste into Word.

sigh - I guess I’ll turn off my bolding for types. I’ve gotten really used to that little bit of highlighting to be able to quickly see all the objects involved.

This one rocks, Jeff. Thanks!

because the bolding is incorrect in many places - or at least it doesn’t match what I see in the code editor. I see the same thing if I paste into Word.

This would not surprise me one bit, as the RTF lacks proper background color information as well.

If only we could go back and time and open a bug on this, since it’s been broken in exactly the same way all the way back to VS.NET 2002.

Thanks Jeff, this code works wonderfully. However, I found that the ASP.NET forums are eating up the colorized spans. I hacked your code a bit to emit FONT color="" tags instead of spans and it works sweet.

Nice work.

I’m trying to do something similar that’s frustrating the heck out of me. I want to programmatically (vb) copy an html table from an asp.net web page into Word or Powerpoint. It sure sounded easy when I started, but so far nothing works. I can get the html of the htmltable object, but pasting it from the clipboard into the receiving app just gives me raw html code instead of a table. I can’t find a single resource on the internet to help! If anyone has a solution please email me. Thanks.

Not sure why, but I get an exception at line 136 where the code sets the clipboard data:

An unhandled exception of type ‘System.Runtime.InteropServices.ExternalException’ occurred in Unknown Module.

Additional information: Requested Clipboard operation did not succeed.

I’m running VS 2005 in a virtual machine and I have IE 7 on my host machine. Any ideas?

Add this to also map non-standard characters (like the Swedish ).

‘— ADD BEFORE “map bold”
’-- map non-standard characters (from ‘e4 to #228;) /Sire 2006-04-05
Dim hex As String = ""
Dim dec As Integer = 0
For Each m As Match In Regex.Matches(rtf, "\’(?hex[a-f0-9][a-f0-9])")
hex = m.Groups(“hex”).Value
dec = Integer.Parse(hex, System.Globalization.NumberStyles.HexNumber)
rtf = Regex.Replace(rtf, “\’” hex, “#” dec “;”)
Next

I just updated the macro

  • includes international character fix (Simon)
  • added “Modern” and “Legacy” HTML methods, to accommodate HTML input boxes that strip out SPAN and DIV (Danny)
  • added “*Text” methods, so you can see the HTML as plain vanilla text in your clipboard if that’s how you want it (Jug)
  • improved speed dramatically on large snippets (tested with entire 1600+ line classes)

I think that accommodates all the feedback so far…

If you’re getting the error “Requested clipboard operation did not succeeed”, try changing the second argument of Clipboard.SetText from TextDataFormat.Text (default) to TextDataFormat.UnicodeText.

Cheers,

-Richard

It’s useful, but it doesn’t seem to be stripping all leading indentation if there is a blank line present in a block, e.g

if (foo)
{
if (bar)
{
this.DoSomething(3);

    this.DoSomething(3);
}

}

will not be stripped.

“VS copies code to the clipboard with bizarro-world RTF formatting instead of the sane, simple HTML markup you might expect. This is true even of the brand spanking new VS.NET 2005.”

Bad news: This is also true in the brand spanking new VS2008 :frowning:
Good news: The macro still works :slight_smile:

Does this work with the enhanced syntax highlighting of Visual Assist X (www.wholetomato.com)?

Thanks Jeff for providing this great tool! I hacked together a workaround for the background highlight color issue. Basically, I hard coded the macro to search for the ASP.NET delimiters and apply CSS styling onto them. Of the code I want to post, the ASP.NET markup is the only type that I highlight. The code is too long to post here, so feel free to copy my “solution” from http://www.alistforeverything.com/post/2008/04/Source-Code-Syntax-Highlighting-in-Blog-Posts-with-FormatToHtml.aspx#continue.

You do not illustrate how to past into word document ???
after creating my macros and pasting my code into it, I want to include this code into word dcouemnt ? how can i do that ??

nice

The copy/paste functionality doesn’t seem to work with VS2008 and Vista x64. I can confirm that the macro doesn’t have any errors, but it doesn’t seem to paste to the clipboard. The clipboard never gets the content.

Thank you Jeff for this awesome macro. I’ve already used it on many occasions without incidence. I was wondering if you still get notified when this thread gets updated since I didn’t see a reply from you since April 7, 2006.

Anyhow, if you are still listening, I was wondering why you wouldn’t use the pre tag instead of the div as a container? Then that way you wouldn’t need to process the white-spaces and line breaks.

Just my two cents as I’ve already customized your macro this way and it works perfectly. Thank you again for sharing your experiences!