VBScript program to convert the code in a specified text file into HTML. The HTML is designed to preserve spacing in the file, including any indented lines, and to display the code with a fixed width font.

In the Microsoft forums you can insert code into a question, discussion, or reply using the "Insert Code Block" feature. At present (October of 2012), this features encloses the code in <pre> and </pre> HTML tags. Documentation indicates that all text within these "preformatted" tags should render in any browser in a fixed width font with all white space maintained. However, this is not the case as of this time. The indenting in the code is greatly reduced and a proportional font is used, making the code more difficult to read. In addition, it is difficult to maintain white space before and after the code block, and it is difficult to add more text after the code block without it being included in the code block.

The Microsoft forums also have an "Edit HTML Source" feature, which allows you to edit your message in HTML. It can be very difficult to edit a message manually in HTML. However, the feature allows you to paste code formatted as HTML into the message that maintains all spacing and uses a fixed width font. This program was developed to produce the HTML that can be pasted directly into such a forum message using the "Edit HTML Source" feature.

This program accepts one parameter, which is the text file containing the code. This can be a short code snippet demonstrating a concept, or a complete program. This program outputs the text in the file converted to HTML into another file with the same name, but with the extension *.htm. The new file is created in the current folder, no matter what the path is of the input file. You can then open this new text file in Notepad, copy the HTML, and paste it into your forum message using the "Edit HTML Source" feature. After you paste the HTML, what you see in the edit window will not look pretty, as all carriage returns in the *.htm file are removed. However, the appropriate tags exist to make the code render correctly in the message after you click "Update". An example of the syntax to use this program at a command prompt would be as follows:

cscript CodeToHTML.vbs Example.vbs

The new file,  Example.htm in this case, is created in the current folder. The program ensures that each line of the code in the HTML is terminated with a carriage return. The  program inserts spaces (using the &nbsp; HTML tag) as required to maintain any indenting exactly as found in the input file. The program specifies that either the "Courier New" font (if available) or the "Courier" font should be used, both of which are fixed width. The program also replaces any instances of characters that can cause problems in HTML into the appropriate equivalents. The following table documents these substitutions.

Character HTML Equivalent
& &amp;
< &lt;
> &gt;
" &quot;
' &#39;

Finally, the program adds tags at the end for a new paragraph, in case you want to add anything after the code. After you paste the HTML into the "Edit HTML Source" window, you can return from the HTML edit mode by clicking the "Update" button and continue to edit your message, including adding text after the code block. The result is code that is much easier to read. However, the program does not validate the VBScript program. It is not designed to check for syntax errors. It is assumed that the code is valid with no syntax problems.

CodeToHTML.txt <<-- Click here to view or download the program

Update June, 2011: There was an update deployed to the Microsoft forums on June 9, 2011. It fixed several bugs. One thing I find fixed is that you can now enter further text after you use the "Insert Code Block" feature, without it appearing in the code block. However, the feature still uses <pre> and </pre> HTML tags. Indenting is not preserved and the font is not fixed width. Also, if you submit your reply, and then later edit your reply, the indenting in your code block is further reduced. All of these problems are avoided by using this program to convert your code into HTML and using the "Edit HTML Source" feature to paste it into your reply.

Also, if you use Notepad to create your text file with the code, be sure to save it as ANSI and not Unicode. Certain files, such as ones where the first line begins with the single quote character, "'", default to Unicode. You don't see it in Notepad, but the text file is actually saved in Unicode (two bytes per character), and the resulting HTML file will look as if all characters are separated by blank spaces. If your HTML looks spaced out, open your original text file in Notepad, select File, SaveAs, then select ANSI for the Encoding (near the bottom of the dialog) and save.

Update July, 2011: The program has been modified to add a short preformatted section at the end as follows:


This ensures that when the HTML is inserted in the forum message, the posting will be recognized as containing code. Without this addition, the posting would not be flagged as having code, so you would not get credit in the new Microsoft Forums recognition system for having replied to or answered a question with code. The only remaining drawback is that you cannot select a language for the code snippet and have it "colorized" to indicate keywords, comments, etc. I personally think having  legible code is more important.

Update October, 2011: Another program has been developed with the same functionality except that it also "colorizes" the code. The code is assumed to be VBScript. It is also assumed that there are no syntax errors. Keywords are colored blue, comments are colored green, and quoted strings are colored red. This is designed to match the colorization provided by the "Insert Code Block" feature in the Microsoft Forums when you select "VB.NET" as the language (there is no "VBScript" option).

VbsToHtml.txt <<-- Click here to view or download the program

And the following program does the same for PowerShell scripts. This program works for most PowerShell code. However, PowerShell is more difficult to parse and there are cases where this program doesn't handle the code correctly. It works best if keywords and operators are delimited by spaces.

PsToHtml.txt <<-- Click here to view or download the program

Update February 8, 2012: Both programs have been updated to include a solid border around the block of code, to match the Insert Code Block feature of the new forum editor just deployed to the forums. Also, all embedded spaces are maintained, such as two or more space characters in a row.

Update February 18, 2012: There has been another forum update, after which the code resulting from these programs does not use a fixed-width font. The solution has been to remove the border around the code. It is not known why that works. In place of the border, a horizontal line has been added, to help separate the code from the rest of the forum reply.

Update February 23, 2012: After another forum update, it appears we can again specify a border. The scripts have been revised to specify a solid gray border, and to not add the horizontal line. The solid gray matches the border used for the <pre> tags.

Update April 27, 2012: Once again when a solid gray border is specified in the HTML, the style is ignored, so the forum reply no longer uses the specified fixed-width font. Since this seems to happens periodically, the code for all three scripts has been modified to accept an optional second parameter (the first parameter is the name of the text file to be converted to HTML). If the second parameter is "/nb", the scripts will not specify the gray border. Without this parameter, the scripts will specify the gray border. If "/nb" is specified, a horizontal line is added at the top of the code to separate it from previous text.

Update July 13, 2012: Another script has been developed to convert a specified Excel spreadsheet into an HTML table. This can be used to convert a spreadsheet into HTML that can be pasted into a TechNet Wiki article. The script accepts the spreadsheet file name as a parameter. The first row of the spreadsheet is assumed to be column headers. The computer where the script runs must have Microsoft Excel installed. The script creates a new file with the same name as the spreadsheet file, but with extension *.htm.

XLToHtml.txt <<-- Click here to view or download the program