Word – Format text as code snippet in any word processor, like we do in Stackexchange sites

automationformattingmicrosoft word

Its been more than six months since I'm using stackexchange, so everyone out there knows that we're speechless about how much it has been helpful to us. But a small thing about the these sites that I'm in love with is that the way we can format our questions, I'm pointing particularly to the way we format code snippets, i.e. either keeping them in single quotes (I don't know if they're actually called quotes or something else) LikeThis.Code() or by prefixing four spaces.

I need to write most of my college work in Microsoft Word or OneNote, where I have large chunk of code to be placed, I like my writing organized, like everyone do, so I format those code snippets with mono fonts manually. But after coming to stack… sites, I was wondering if I can automate similar behaviour in Word through some third party program, like AutoHotkey or something else. While I know AutoHotkey deals a lot with keyboard, but I was looking for a more fast and efficient way to format block of words into mono fonts (its ok, I'm not expecting them to hightlight syntax). Before a few months, I read an article on HowToGeek about an application that automatically replaces short-forms of words that we often type to the full word, basically converting informal English to formal (I can't remember now what was that application). So with the same way, can I format text in mono fonts, using single quotes or something else? so that I don't have to manually select text and change fonts to mono.

Best Answer

Create a new style that formats the text the way you want. The style will likely need to include:

  • setting the font to a monospaced font like Courier New
  • setting the language to No Proofing (Word 2003 and older) or Do Not Check Spelling or Grammar (Word 2007 and newer)
  • setting the font size to something smaller; often code won't fit well on a page unless you make it smaller

To use this, select the text and change the style. All the formatting attributes will be applied at once.

Save the style in the normal.dot template (the default) so that it's available in all documents.

If you need to do this often, you can assign a keyboard shortcut to the text so you don't have to select the style from the style menu. You don't need a separate program to do this. In Word 2007

  • Click the Office button.
  • Click the Word Options button (at the bottom of the menu)
  • Select Customize from the menu on the left.
  • Click the Keyboard Shortcuts: Customize button at the bottom.
  • In the Categories list, scroll down until you find Styles (second from the bottom) and select it.
  • In the Styles list, select the style you created.
  • Click in the Press New Shortcut key box.
  • Type the key combination you wish to use (e.g. Alt 1)
  • Click the Assign button
  • Click Close

When you close Word it may ask if you want to save the normal template. Answer Yes.