Formatting HTML code snippets with Ten Ton Wrecking Balls

If you've ever tried to cut and paste code from the VS.NET IDE, you may have noticed that the code generally comes across looking like crap. The root of this problem is that VS.NET copies code into your clipboard in the accursed Rich Text Format. If you were expecting something like standard HTML, think again, bucko!

This is a companion discussion topic for the original blog entry at:

For an even more hard-core “convert the RTF to HTML our own damn selves” solution, try the excellent VS.NET add-in CopySourceAsHtml:

This is far more sophisticated and feature-rich than my little lightweight RTF to HTML function.

Thanks for the plug! I’m glad to see you had the same epiphany I did. :slight_smile:

Yeah, it’s really nice work! I was looking at the source the other day while working on this.

I happen to be looking at DasBlog right now. Looks like a colorizing regex engine, on a popup form dedicated to that purpose.

It’s using AylarSolutions.Highlight.Highlighter

dasBlog includes an Insert Code toolbar button on it’s implementation of FreeTextBox that does the formatting for you. You’d probably have to contact Scott Hanselman to find out who wrote it or if you could get it though (assuming you use FreeTextBox).

I use the a href=""squishySyntaxHighlighter/a . It’s very nice, preserves collapsable regions and line numbers and is free.

Upgrade to the latest WinZip at … unfortunately I might have saved this with the “extreme compression” that is new to that version of WinZip.

I’ll try to save it in the “compatible compression” and re-upload it.

Hello Jeff,
I have downloaded the zip but can not open it (corrupted).
Please advice,

I created a simpler version of this macro here:

I updated the macro tonight. Most of the improvements are in UsingRtfConversion:

  • Works under VS.NET 2005 (Thread.ApartmentState.STA must be manually specified when accessing the clipboard)
  • Wraps the code snippet in a DIV
  • Sets the background color of the code
  • Minor HTML formatting improvements
  • Option to remove first TAB for heavily indented code (this could be automated, hmm…)

The Word functionality is unchanged!

I had to restart my IDE before the macro would work. Dunno if that’s something particular to my environment or what, but thought it was worth mentioning if someone else has any problems.

Using the RTM of VS 2005 I found that I had to add a reference to System.Web in order to get this to owkr. After that point, it worked great. Thanks for the tool!

Ok, I give up. I installed the macro but now how do I use it?


First, you should be using the new, simpler macro here:

Second, once you’ve installed the Macro, either map it to a keyboard key (Tools, Options, Keyboard) or just double-click on it in the Macro Explorer.

Hi Anand, you need to add a reference to the System.Web class in the Macro IDE-- you can do this via the “add reference” menu.

I’m unable to use your tool. It is full of errors. I’m getting Regex error (around 21 in number)

Can you provide a step-by-step installation instructions?

It is now working but there is an error in the code

rtf = Web.HttpUtility.HtmlEncode(rtf)

Error 1 ‘HttpUtility’ is not a member of ‘Web’.

Is there any way to fix this error?

Personally I don’t think they are that bad.
Fantastic thank you. Find the files you are looking for at the most comprehensive source for free-to-try files downloads on the Web