How to format code blogging with word 2007

Today I was trying to blog with word 2007 and I found some difficulties with code. First of all I suggest you to install a Visual Studio Plugin that enable a “copy source as HTML” option that permits you to copy the code as HTML and this makes the life easier. The problem is that simply pasting the code into word will produce some strange results

    3 ”’ <summary>

    4 ”’ classe base per la definizione di proprietà comuni

    5 ”’ </summary>

    6 ”’ <remarks></remarks>

    7 <TypeConverter(GetType(ExpandableObjectConverter))> _

    8 Public
Class BaseDefinition

    9 
   17    <NotifyParentProperty(True)> _

   18   Public
Property CssClass() As
String

   19      Get

   20         Return mCssClass

   21      End
Get

   22      Set(ByVal value As
String)

   23          mCssClass = value

   24      End
Set

   25   End
Property

The code above has two problems, the first is that spacing between lines is too big, and then in HTML there are some <BR/> tags that completely makes code unreadable. After some investigation I found that it is possible to force word to generate better HTML making two substitutions. The first is substitute ^p character with ^l, the ^l character does not generate <P> tags but a simple <BR/> and this makes lines spacing smaller.

    3 ”’ <summary>
    4 ”’ classe base per la definizione di proprietà comuni
    5 ”’ </summary>
    6 ”’ <remarks></remarks>
    7 <TypeConverter(GetType(ExpandableObjectConverter))> _
    8 Public
Class BaseDefinition
    9 
   17    <NotifyParentProperty(True)> _
   18   Public
Property CssClass() As
String
   19      Get
   20         Return mCssClass
   21      End
Get
   22      Set(ByVal value As
String)
   23          mCssClass = value
   24      End
Set
   25   End
Property

Now you need to substitute also the space character with ^s (unifying space) this (do not ask me why) makes word not to generate the weird <BR /> tag.

    3 ”’ <summary>
    4 ”’ classe base per la definizione di proprietà comuni
    5 ”’ </summary>
    6 ”’ <remarks></remarks>
    7 <TypeConverter(GetType(ExpandableObjectConverter))> _
    8 Public Class BaseDefinition
    9 
   10    ”’ <summary>
   11    ”’ è necessario questo attributo per far siche il designer si accorga del 
   12    ”’ cambiamento della proprietà e fare in modo che venga serializzata
   13    ”’ </summary>
   14    ”’ <value></value>
   15    ”’ <returns></returns>
   16    ”’ <remarks></remarks>
   17    <NotifyParentProperty(True)> _
   18    Public Property CssClass() As String
   19       Get
   20          Return mCssClass
   21       End Get
   22       Set(ByVal value As String)
   23          mCssClass = value
   24       End Set
   25    End Property

Now the code looks really better but a last problem remains, the code with line numbering cannot be copied and pasted from the blog into visual studio. Even if you remove the numbering, word substitutes all occurrence of double ‘ chars with ” character, so the resulting VB.Code is not valid. At this time I really do not understand how to tell word to leave the ‘ code unchanged.

Alk.

Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.