Pasting nicely formatted code into your blog

BTW, I was asked how I put the code so nicely formatted on my blog. Here are the steps:
  • Write the code in vs.net
  • Paste it into a new word document
  • Save the document as an html(filtered) (or .Text will have trouble with it)
  • Close the doc , then re-open it without changing any text after that (this is an important step)
  • Copy the code from the doc into your blog post
 
Update: ok. This was just too obvious. I've created a short word macro that does this for you.
simply paste the following code in a new macro module in word that belongs to the Normal.dot template.
Then assign it a keyboard shortcut using right click on toolbars-customize-keyboard-macros and you're done:
 

Sub FormatCodeForBlog()

    Selection.PasteAndFormat(wdPasteDefault)

    ActiveDocument.SaveAs(FileName:="temp.htm", FileFormat:=wdFormatFilteredHTML, _

          LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _

        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _

        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _

        False)

    ActiveWindow.View.Type = wdWebView

    ActiveWindow.Close()

    RecentFiles(1).Open()

    Selection.WholeStory()

    Selection.Copy()

End Sub

Published Thursday, February 05, 2004 2:08 PM by RoyOsherove
Filed under:

Comments

Thursday, February 05, 2004 7:46 AM by Roy Osherove

# re: Pasting nicely formatted code into your blog

Steven, np :) I removed the comment.
Comment removed - posted in the wrong place.
Thursday, February 05, 2004 8:05 AM by Scott Galloway

# re: Pasting nicely formatted code into your blog

Man this is useful...I have had so much trouble with this in the past...never thought of using Word. I used to use http://www.manoli.net/csharpformat/ , but your version is so much easier, thanks again Roy!
Thursday, February 05, 2004 12:20 PM by Robert McLaws

# re: Pasting nicely formatted code into your blog

You can also open an RTF e-mail in Outlook and accomplish the same result.
Friday, February 13, 2004 10:16 AM by TrackBack

# Formatting COde in Your Blog

Roy Osherove wrote a quick MS Word macro that allows you to take your VS.NET code and paste it as good-looking, formatted code into your blog...
Saturday, February 21, 2004 9:38 PM by Tone Engel

# TonesNotes let's you post directly from Word.

You could also try an add-in for Word XP or Word 2003 that let's you post directly from word. No cutting and pasting. All of Words formatting power (tables, code formatting) and a one click post to your .Text powered blog.

With a minor mod to .Text it also handles embedded images (with or without thumbnails) with no extra work. One click, the images go, the post goes, and you get an archived copy saved locally that you can edit and repost at any time.

Enough of a shameless plug. The code is still at an alpha level, but I'm anxious for some brave souls who want to give it a try!

http://workspaces.gotdotnet.com/TonesNotes
Tuesday, March 30, 2004 5:43 AM by TrackBack

# Paste properly formatted code into your blog

Tuesday, March 30, 2004 5:43 AM by TrackBack

# Paste properly formatted code into your blog

Friday, April 02, 2004 2:45 PM by TrackBack

# re: Posting source code...any clues?

Wednesday, April 14, 2004 8:05 PM by TrackBack

# Well Formatted Blog, Can't Directly Copy from MS Word

Tuesday, May 11, 2004 8:34 AM by DotWind Solutions

# re: Pasting nicely formatted code into your blog

I did something similar but to format SQL code can be found here

http://blog.dotwind.com/archive/2004/05/11/148.aspx
Monday, June 07, 2004 12:30 AM by val

# re: Pasting nicely formatted code into your blog

I pasted the code into appleworks, saved as html,re-opened, pasted into my blog, what showed up on my page was the exact code that I had pasted. Previously I pasted the code into the tag line when I was in Prefs: appearances and it worked fine but I do not want it there. Any more ideas.
Wednesday, October 13, 2004 10:53 AM by TrackBack

# Visual Studio Add-In: CopySourceAsHtml

<p>
<a href="http://blogs.msdn.com/brada/archive/2004/10/05/238427.aspx">Brad Abrams</a>,
<a href="http://blogs.msdn.com/jaybaz_ms/archive/2004/03/30/103505.aspx">Jay Bazuzi</a>,
<a href="http://weblogs.asp.net/rosherove/archive/2004/02/05/67957.aspx">Roy Osherove</a>,
and what seems like the majority of .NET bloggers use Word, Word macros, or Visual Studio macros that use Word
in some way to format their code. A few use
<a href="http://www.manoli.net/">Jean-Claude Manoli</a>'s
<a href="http://www.manoli.net/csharpformat/">CSharpFormat</a>
tool. Each solution has drawbacks (opening Word, opening another browser window, copying and pasting, extra steps,
terrible HTML, etc) and formatting code is still painful. So...
</p>
<p>
I've written a simple Visual Studio add-in that allows you to copy source as HTML suitable for pasting into blogs...
</p>
Tuesday, October 19, 2004 7:52 AM by TrackBack

# CopySourceAsHtml

Brad Abrams, Jay Bazuzi, Roy Osherove, and what seems like the majority of .NET bloggers use Word, Word macros, or Visual Studio macros that use Word in some way to format their code. A few use Jean-Claude Manoli's CSharpFormat...
Friday, November 12, 2004 9:13 AM by TrackBack

# Superb Blog-Related Marketing

Hilton Giesenow says I have some superb blog-related marketing going on. I guess we all become Robert Scoble in time. I must just have an accelerated case of the Scobles. :) Seriously though, I have been marketing. My first...
Sunday, November 14, 2004 10:42 AM by TrackBack

# CopySourceAsHtml

Monday, December 27, 2004 3:25 PM by TrackBack

# CopySourceAsHtml

Brad Abrams, Jay Bazuzi, Roy Osherove, and what seems like the majority of .NET bloggers use Word, Word macros, or Visual Studio macros that use Word in some way to format their code. A few use Jean-Claude Manoli's CSharpFormat...