The past few blog posts that I have made had some code in them, and the formatting was lost. I am using Word 2007 for posting to my blog (like a good corporate citizen), but was just about to give up. The blog posts are really hard to read when you have unformatted code in them.

Today I heard about Colin Coller's CopySourceAsHTML. It adds a context menu item so that when you right-click on source in Visual Studio 2005, it will copy the source as HTML. Then you can paste the source into Word 2007, and you get some semblance of code formatting.

You can control whether you get line numbers or not, override CSS, and a few other cool features. The only shortcoming that I see is that it doesn't seem to support ASPX files yet. This tool is really impressive in that you can copy XML, C++, and J# just as easily as C# or VB.NET. Bravo, Colin!

Here are some results of copying from Visual Studio 2005 using CopySourceAsHTML and pasting into Word 2007.

XML

<?xml version="1.0"?>

<configuration>

  <system.serviceModel>

    <services>

      <service behaviorConfiguration="returnFaults" name="service2">

        <endpoint binding="basicHttpBinding" contract="Iservice2"/>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="returnFaults">

          <serviceDebug includeExceptionDetailInFaults="true"/>

          <serviceMetadata httpGetEnabled="true"/>

          <MyBehavior/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <extensions>

      <behaviorExtensions>

        <add name="MyBehavior" type="WCFExtensions.MyBehaviorExtension, WCFExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

      </behaviorExtensions>

    </extensions>

  </system.serviceModel>

  <system.diagnostics>

    <switches>

      <add name="XmlSerialization.Compilation" value="1" />

    </switches>

  </system.diagnostics>

</configuration>

 

C++

// RealEstate.cpp : main project file.

   

#include "stdafx.h"

#include "Property.h"

   

using namespace System;

   

int main(array<System::String ^> ^args)

{

    Console::WriteLine(L"Hello World");

    return 0;

}

 

J#

package WindowsService1;

   

import System.Collections.Generic.*;

import System.ComponentModel.*;

import System.Data.*;

import System.Diagnostics.*;

import System.ServiceProcess.*;

   

/**

 * Summary description for Service1.

 */

public class Service1 extends System.ServiceProcess.ServiceBase

{

    /**

    * Required designer variable.

    */

    private System.ComponentModel.IContainer components;

   

    public Service1()

    {

        //

        // This call is required by the Windows.Forms Component Designer.

        //

        InitializeComponent();

   

        //

        // TODO: Add any initialization after the InitComponent call

        //

    }

   

    #region Windows Form Designer generated code

    /**

    * Clean up any resources being used.

    */

    protected void Dispose(boolean disposing)

    {

        if (disposing)

        {

            if (components != null)

            {

                components.Dispose();

            }

        }

        super.Dispose(disposing);

    }

   

    /**

    * Required method for Designer support - do not modify

    * the contents of this method with the code editor.

    */

    private void InitializeComponent()

    {

        components = new System.ComponentModel.Container();

        this.set_ServiceName("Service1");

    }

    #endregion

   

    /**

    * Set things in motion so your service can do its work.

    */

    protected void OnStart(String[] args)

    {

        //

        // TODO: Add code here to start your service.

        //

    }

   

    /**

    * Stop this service.

    */

    protected void OnStop()

    {

        //

        // TODO: Add code here to perform any tear-down necessary to stop your service.

        //

    }

}