Here’s a tip for anyone who’s using Windows Live Writer for blogging and wants to paste code into the articles.
Ever since my first code post back in July 2008 I have tried about four different Live Writer plug-ins to get the code formatting right. Three of those are still installed today and I regularly use two of them. As I post code samples in several languages (SQL, MDX, C#, VB, XML, …) I need the plug-in to support all those languages and to turn the code into decent output. It would be ideal if I could do all this with just one plug-in.
Disclaimer: the code shown in this article is copied from my own blog or from Microsoft code samples. They are not guaranteed to be complete and are just for demonstration purposes. No explanation is provided on what the code is supposed to be doing either.
The Old Stuff
Following three screenshots will show you those three plug-ins in action.
Code Snippet
More info through this link and even more info on the author’s blog.

Colorized Code
More info through this link

Source Code Formatter
More info through this link

This last one is the one that I don’t use very often – to be honest I don’t even remember what I’ve actually used it for…
Now, back to the real reason for this article.
Time For Something New
As I mentioned before, I’m not extremely happy with the fact that I need to use several plug-ins for a similar task. I keep forgetting which one the more interesting one is for SQL, or for C#, or whatever, I think you get my point. It would be perfect if one plug-in could do all this. And it has taken me quite a while but I think I may finally have found that one plug-in. You can see it in action in my previous post. Doesn’t that look nice?
What I like most about this plug-in is that the developer has taken a different approach to get the formatting done. All other plug-ins that I’ve tried required me to paste the code into a pop-up window and select the right programming language, as I’ve shown you above. This one is much simpler: it takes what is stored in the clipboard, including the formatting!
In my opinion this has two interesting advantages:
- Faster: no messing around with a pop-up window
- Accurate: as it uses the formatting as provided by the application from which it was copied, it will look exactly the same! Which is something that I can’t say about the other plug-ins.
Okay, this means I don’t have any other options either, no line numbering, no alternating background coloring, … But who cares when the output looks good? And I don’t use those extra features anyway.
Here are some pasted snippets using my new favorite plug-in.
SQL copied from the Management Studio (SSMS 2008)
--a comment
declare @number int = 42;
declare @character char(1) = '0';
declare @expectedLength int = 8;
/* a block comment */
select REPLICATE(@character, @expectedLength - LEN(@number))
+ CAST(@number as varchar(8)) as Result;
MDX Copied From SQL Server Management Studio (SSMS 2008)
SELECT NON EMPTY { [Measures].[Reseller Sales Amount], [Measures].[Reseller Order Quantity] } ON COLUMNS,
NON EMPTY { ([Geography].[Geography].[Postal Code].ALLMEMBERS ) }
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Adventure Works]
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
C# Copied From Visual Studio (VS 2008)
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void WeatherSP(string zipCode)
{
Weather myWeatherWS = new Weather();
WeatherReturn weatherResult = myWeatherWS.GetCityWeatherByZIP(zipCode);
SqlMetaData[] recordMetaData = new SqlMetaData[2];
// layout of the records that we'll return
recordMetaData[0] = new SqlMetaData("Description", SqlDbType.Char, 100);
recordMetaData[1] = new SqlMetaData("Value", SqlDbType.Char, 1000);
// build a record based on the metadata
SqlDataRecord record = new SqlDataRecord(recordMetaData);
// let's start sending result into the active pipe
SqlContext.Pipe.SendResultsStart(record);
XML Copied From Visual Studio (VS 2008)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyWebService" value="http://MyWebServiceServer:MyWSPort/PathTo/MyWebService.asmx" />
</appSettings>
</configuration>
VB.NET Copied From Visual Studio (VS 2005)
#Region "AddColumnInformation"
'/ <summary>
'/ Stores the column information of the provided input and output objects in a ColumnInfo object.
'/ This method is called by the RemoveDuplicates component during PreExecute when the BufferManager
'/ is available, and assumes the exact same number of input and output columns.
'/ </summary>
'/ <param name="bufferManager">The IDTSBufferManager100; used to locate the columns
'/ in the input and output buffers.</param>
'/ <param name="output">The output of the component.</param>
'/ <param name="input">The input of the component.</param>
<System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")> _
<System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId:="System.Exception.#ctor(System.String)")> _
<CLSCompliant(False)> _
Public Sub AddColumnInformation(ByVal bufferManager As IDTSBufferManager100, ByVal output As IDTSOutput100, ByVal input As IDTSInput100)
If input Is Nothing Then
Throw New ArgumentNullException("input")
End If
If output Is Nothing Then
Throw New ArgumentNullException("output")
End If
If bufferManager Is Nothing Then
Throw New ArgumentNullException("bufferManager")
End If
If input.InputColumnCollection.Count <> output.OutputColumnCollection.Count Then
Throw New Exception("Input column collection does not match the output column collection.")
End If
For x As Integer = 0 To output.OutputColumnCollection.Count - 1
Dim colInfo As New ColumnInfo()
Dim outCol As IDTSOutputColumn100 = output.OutputColumnCollection(x)
Dim inCol As IDTSInputColumn100 = input.InputColumnCollection(x)
' Set the buffer column index for the input column and the output column.
colInfo.inputBufferColumnIndex = bufferManager.FindColumnByLineageID(input.Buffer, inCol.LineageID)
colInfo.outputBufferColumnIndex = bufferManager.FindColumnByLineageID(output.Buffer, outCol.LineageID)
' Save the column
columnInfos.Add(colInfo)
Next x
End Sub
#End Region
End Class
One feature though that I’d like to see is a horizontal scrollbar when the code lines are longer than the width of the code block. Right now it automatically wraps the text which makes it a bit difficult to read. So I have to pay attention when pasting code. Although it’s not always obvious, you can see this wrapping occur in three of the above code snippets (MDX, XML, VB.NET).
To conclude this article: the new plug-in that I’m using is called Paste from Visual Studio (or VSPaste), developed by Douglas Stockwell. You can download it here.
Out of curiosity, I wonder how it performs when pasting formatted text from Word (2007):
PrFont34Bin0BinSub0Frac0Def1Margin0Margin0Jc1Indent1440Lim0Lim1Some bold text
And some italic
Italic, bold and underline in combination
Text in blue and green and red on one line
Okay, ow, look at that, I’m writing in red now, that can’t be the intention! Luckily Live Writer allows me to switch back to black.
Update: the “writing in red” was apparently only visible in Live Writer…
So, conclusion: VSPaste not only works when pasting from Visual Studio, SQL Server Management Studio is working perfect as well! But Word is not. But that’s not a problem 
Happy blogging!
Valentino.