Posts Format Code to HTML within a VS.NET 2K3 Macro (leveraging Microsoft Word)

Format Code to HTML within a VS.NET 2K3 Macro (leveraging Microsoft Word)

Brad Abrams pointed out that he uses copy and paste into Word from Visual Studio 2005 Express Edition.   Mike Parsons commented that he uses a VS.NET macro to help automate the same task.  I really liked the idea of doing this directly within VS.NET.

After looking through it, I found what I thought were some flaws in the implementation; not removing the file upon finishing the task being one of them causing a lot of temporary files being created and the fact that he wasn't leveraging some System.IO methods to handle the creation of the temporary file.  In addition, you still had to switch to another task (your system default browser) in order to select the text, yet again, to paste it in your blogging software.  In addition, the final HTML was still pretty ugly.  I decided to fix some of these deficiencies. 

I think this macro pretty solid, however, I've not really done a whole lot of dogfooding just yet.  If you find any problems, please let me know. 

Here is the final product.

Option Explicit On

Option Strict Off ' Using late binding.


Imports System

Imports EnvDTE

Imports System.Diagnostics


Public Module Module1


  Private Const WdPasteDefault As Integer = 0

  Private Const WdFormatFilteredHTML As Integer = 10

  Private Const WdWebView As Integer = 6

  Sub FormatSourceCode()


    ' Get the currently selected code snippet.

    Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), TextSelection)


    ' Check that something is selected.

    If selection.Text = "" Then

      MsgBox("No code selected!", MsgBoxStyle.Critical Or MsgBoxStyle.OKOnly, "Format Code")


    End If


    ' Create a temporary file.

    Dim path As String = System.IO.Path.GetTempFileName()


    ' Copy the selected code to clipboard (using VS.NET).



    ' Instantiate a new Word document to achieve HTML code formatting.

    Dim oleDocument As Type = Type.GetTypeFromProgID("Word.Document")

    Dim document As Object = Activator.CreateInstance(oleDocument)


    document.SaveAs(path, WdFormatFilteredHTML, False, "", True, "", False, False, False, False, False)


    document = Nothing

    oleDocument = Nothing


    ' Open a new instance of Word.

    Dim oleApplication As Type = Type.GetTypeFromProgID("Word.Application")

    Dim application As Object = Activator.CreateInstance(oleApplication)

    ' Open the temporary document.

    document = application.Documents.Open(path)

    ' Switch to the WebView mode within Word.

    document.ActiveWindow.View.Type = WdWebView

    ' Select the whole document.


    ' Copy it to the clipboard.


    ' Close the document.


    document = Nothing
    <FONT color=#000000>application.Quit()</FONT>

    application = Nothing

    oleApplication = Nothing


    ' Cleanup after ourselves.



  End Sub


End Module

And yes, I did use this macro to format the code within here ;-)

Note: I tried a couple of methods to get the final result saved within the clipboard... the one I settled on produced the best result.  However, because it has to launch a new instance of Word; it takes a second or two.  Still faster than doing everything by hand :-D

[update] Josh Ledgard pointed out a nice step by step process for adding macros in Visual Studio.  Also, I'm not an expert in the Microsoft Word object model.  I should point out that I've only tested this with Microsoft Word 2003.  What other versions of Microsoft Word this macro works with (or doesn't) is anyones guess.  If your using other versions of Microsoft Word, please let me (and others) know whether it's working or not by leaving a comment.

[update] Modified the code listing to include the application.Quit() call.

[update] Modified the code to conform to the VB.NET Coding Guidelines, add it to the code samples / articles document and created a text file version of the macro for download.

This post is licensed under CC BY 4.0 by the author.