233

As a programmer at a big corporation, I frequently send Outlook emails that contain code samples.

I'll actually type code directly into an email. This inevitably causes problems, as Outlook really likes to format text in pleasing but unhelpful ways. My code needs to be copyable out of the email and directly into code, so I don't want Outlook to mess with it by adding special characters or whatnot.

So I always need to tweak options like:

  • Don't capitalize first letter of every sentence (else all my functions be Public instead of public)
  • Disable smart quotes (fancy quotes don't copy+paste out of emails well)
  • Never use spell checker (because it just gets annoying)

I also like my code to be in a monospaced font, black, indented, and smaller than other text. I've tried to make a Style for this, but for some reason the Style never saves. :-( [edit: turns out I wasn't telling it to work on "new documents based on this template"]

Is there some way to put code in <pre> blocks or something that tells Outlook to ignore all these rules and format code the way I want it to? If not, what ways have you found to send code snippets in Outlook emails without it getting super annoying?

Mag Roader
  • 6,850
  • 8
  • 32
  • 27

12 Answers12

164

Years later I have a response.

  1. Use an online code highlighter like http://tohtml.com/ to highlight your code so you can paste marked up code from your IDE into Word. Depending on your IDE you may be able to skip this step.

  2. In Word 2010, go to insert->object->openDocument Text. Steps 2-3 are documented at How do you display code snippets in MS Word preserving format and syntax highlighting?.

  3. Paste your highlighted code into the object.

  4. Copy the whole object.

  5. Right-click-> paste special the object into Outlook.

This gives you a highlighted, contained box of code for use in emails in Outlook 2010.

Community
  • 1
  • 1
trueshot
  • 1,742
  • 2
  • 11
  • 8
  • 9
    you can also use tohtml to generate the formatted output, then just copy the preview directly (it's next to the formatted code box). – gebuh Jun 13 '14 at 16:57
  • 1
    You should be a bit careful with online conversion tools like that. They might be "free", but there is a good chance that they store / process / sell anything you submit. After all, they need to make money. You wouldn't want restricted code you wrote for your company to be stored on external servers and handled like that. Better be safe than sorry... – mzuther Nov 30 '22 at 15:19
  • 1
    @mzuther I agree, I would not put sensitive code into such a tool. It is best used with generic, unidentifiable snippets. With that said, one can monitor via browser tools for any network activity. – trueshot Dec 03 '22 at 17:08
162

If you have Notepad++ installed in your PC, then you can copy text as RTF (Rich Text Format) and paste it in your outlook mail.

  1. Paste your code snippet into Notepad++

  2. From Menu bar navigate to "Plugins -> NppExport -> Copy RTF to clipboard"

  3. Paste into your email

  4. Done

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
srujan reddy
  • 1,621
  • 1
  • 9
  • 5
  • 7
    Easiest way possible if your're using Notepad++. And let's be honest, as a programmer one should be ashamed for not using it. :) Edit: By the way, you can even set some nice easy to use key binding for this. – f1v3 Feb 08 '17 at 13:33
  • 1
    Pure awesomeness, kudos to Notepad++ as usual. I used to copy-paste the snippets exactly from this (my fave) text editor, so works best for me, even greater than the most voted-up answer which uses tohtml.com as an extra step..! – RAM237 May 14 '18 at 10:31
  • Thanks, is a bit of pain if you have npp 64bit, no plugin manager, some fixes here - https://github.com/notepad-plus-plus/notepad-plus-plus/issues/3264#issuecomment-427022198 – cardamom Nov 05 '18 at 10:41
  • 5
    I found this a little easier to remember: 2) Right-click menu -> Plugin commands -> Copy Text with Syntax Highlighting – Ben Feb 05 '19 at 18:14
  • 3
    Also possible: "Copy HTML to clipboard". HTML is preferred over RTF because of its universality, whereas RTF is "only compatible with Microsoft Outlook and Microsoft Exchange" (according to tooltip in Outlook for Office 365, Version 1808). – sgrubsmyon Jul 11 '19 at 10:32
  • On Mac you can use *Sublime Text* (somehow of Notepad++ equivalent), select the code you want to copy, `Edit>Copy as HTML` and paste it where you want to code formatting. – joepol Apr 17 '23 at 13:15
  • To save a few clicks in Step 2 one could map a shortcut to that option, I personally use `Ctrl + Shift + C` indicating a special copy. I would also like to mention that in Notepad++ you can set highlighting without even saving the file using the Language menu. All hail the god ! – Some Guy May 30 '23 at 18:13
54

If you are using Outlook 2010, you can define your own style and select your formatting you want, in the Format options there is one option for Language, here you can specify the language and specify whether you want spell checker to ignore the text with this style.

With this style you can now paste the code as text and select your new style. Outlook will not correct the text and will not perform the spell check on it.

Below is the summary of the style I have defined for emailing the code snippets.

Do not check spelling or grammar, Border:
Box: (Single solid line, Orange,  0.5 pt Line width)
Pattern: Clear (Custom Color(RGB(253,253,217))), Style: Linked, Automatically update, Quick Style
Based on: HTML Preformatted
mehul
  • 682
  • 6
  • 6
  • 3
    This worked great for my purposes, in Outlook 2013. One thing that irked me was that it would stop ignoring spelling/grammar midway through the snippet, so I set the language to be something that Outlook didn't have a spellchecker for (I chose albanian). – Mitch Sep 23 '14 at 20:17
  • 11
    Hi can you tell us how to define our own style? a google found quick styles, but they don't allow the options you mention. – Myster May 26 '15 at 22:05
  • This works great for me except for the fact that it forces me to select a font color which overwrites some of the color of text copied from Visual Studio. Does anyone know if there's a way to omit text color from the style? – Open Kastle Oct 01 '15 at 21:55
  • One note: You might be copying rich text, but outlook may be trying to "match current formatting" if you see a flash of correct formatting, then look for the little clipboard icon after you paste and change it to "match source formatting" – SimplGy Jun 12 '17 at 17:07
  • Thanks for the comments. And I made a similar one with your guidance, with style like github: Font: (Asian) Microsoft YaHei UI, 9 pt, Font color: Black, Do not check spelling or grammar, Indent: Left: 0.5" Right: 0.5", Don't adjust right indent when grid is defined, Border: Box: (Single solid line, Custom Color(RGB(231,234,237)), 0.5 pt Line width) Pattern: Clear (Custom Color(RGB(248,248,248))), Style: Linked, Automatically update, Show in the Styles gallery Based on: HTML Preformatted – LingYan Meng May 05 '19 at 07:18
  • The works except that it disappears after I close the email window. A new email doesn't have this Style anymore. – Frak Jan 17 '20 at 15:13
  • To create or modify a style you need to be in the `body` of a message and then use the Styles area of the Format Text ribbon. https://support.office.com/en-us/article/customize-or-create-new-styles-d38d6e47-f6fc-48eb-a607-1eb120dec563 – Ben Apr 28 '20 at 22:36
  • To achive this setup: 1. Compose a new e-mail -> Format text tab -> Styles group -> Dropdown -> Create a style 2. Style based on -> HTML Preformatted 3. Bottom left check "Automatically update" 4. Bottom left click Format -> Border -> Shading -> Fill -> Custom -> (253, 253, 217) 5. Still in Border: Borders -> Setting = Box, Color = Orange 6. Format -> Language: English; and check "Do not check spelling and grammar" – Alexander Høst Sep 21 '20 at 12:12
  • @Frak To save the Style, select the "New documents based on this template" radio button at the bottom. Then it will show up in future messages – mrtumnus Apr 20 '23 at 14:36
27

I came across this looking for a way to format things better in an email to a co-worker. I ended up discovering that if you copy from Visual Studio Code (FREE) it retains the formatting, highlighting and everything else. This editor works with everything and has modules for every programming language I've ever encountered.

Looks beautiful in the email.

Allen George
  • 383
  • 4
  • 6
17

Here's what works for me, and is quickest and causes the least amount of pain / annoyance:

1) Paste you code snippet into sublime; make sure your syntax is looking good.

2) Right click and choose 'Copy as RTF'

3) Paste into your email

4) Done

tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149
9

When I paste code into Outlook or have sentences containing code or technical syntax I get annoyed by all of the red squiggles that identify spelling errors. If you want Outlook to clear all of the red spellcheck squiggles you can add a button to the Quick Access Toolbar that calls a VBA macro and removes all squiggles from the current document.

I prefer to run this macro separate from my style choice because I often use it on a selection of text that has mixed content.

For syntax highlighting I use the Notepad++ technique already listed by @srujanreddy, though I discovered that the right-click context menu option a bit handier than navigating the Plugins menu.

Image showing you can right-click on selected text and choose to copy text with syntax highlighting

If you get annoyed by spell check while you are preparing your email you can add a button to your quick access toolbar that will remove the red squiggles from the message body.
See this article: https://stackoverflow.com/a/49865743/1898524

Remove Spell Check Squiggles

Ben
  • 1,168
  • 13
  • 45
7

If you attach your code as a text file and your recipient(s) have "show attachments inline" option set (I believe it's set by default), Outlook should not mangle your code but it will be copy/paste-able directly from email.

ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • 3
    This works, but is not really the workflow I want. I would rather not have to create a file somewhere to attach a small code snippet. Rather, I want to just type directly into the email - perhaps by surrounding some markup or something like I'd do on a wiki. However, from the answers everybody came up with, it appears as if nobody knows of a better way to do this in Outlook at the moment. – Mag Roader Aug 23 '09 at 15:35
6

For me VS Code works best.

  1. Copy the code snippet in VS Code empty file
  2. Save the file with required extension .cpp,.js etc
  3. Copy the code snippet to clipboard
  4. Paste into outlook.
enigma
  • 61
  • 1
  • 3
5

Here is another method with Sublime Text:

  1. Copy into Sublime Text
  2. Format as wanted
  3. Edit --> Copy as HTML
  4. Insert in Mail in Outlook

Tested with Outlook 2202 and Sublime Text 4126.

Pi.Lilac
  • 135
  • 1
  • 7
0

If you do not want to attach code in a file (this was a good tip, ChssPly76, I need to check it out), you can try changing the default message format messages to rich text (Tools - Options - Mail Format - Message format) instead of HTML. I learned that Outlook's HTML formatting screws code layout (btw, Outlook uses MS Word's HTML rendering engine which sucks big time), but rich text works fine. So if I copy code from Visual Studio and paste it in Outlook message, when using rich text, it looks pretty good, but when in HTML mode, it's a disaster. To disable smart quotes, auto-correction, and other artifacts, set up the appropriate option via Tools - Options - Spelling - Spelling and AutoCorrection; you may also want to play with copy-paste settings (Tools - Options - Mail Format - Editor Options - Cut, copy, and paste).

Alek Davis
  • 10,628
  • 2
  • 41
  • 53
  • This works too, but I still need to go change the font to Courier New and indent it and change it to black and whatnot. – Mag Roader Aug 23 '09 at 15:37
  • 1
    Hmmm... I do not have to do this. The only problem I noticed was the tabs, so if your Outlook settings for tabs is different from VS, than tab-based alignments may be a bit off, but that's about it. – Alek Davis Aug 24 '09 at 03:56
0

Would sending the mail as plain-text sort this?

"How to Send a Plain Text Message in Outlook":

  • Select Actions | New Mail Message Using | Plain Text from the menu in Outlook.
  • Create your message as usual.
  • Click Send to deliver it.

Being plain text it shouldn't screw up your code, with "smart" quotes, auto-capitalisation and such.

Another possible option, if this is a common problem within the company perhaps you could setup an internal code-paste site, there's plenty of open-source ones around, like Open Pastebin

dbr
  • 165,801
  • 69
  • 278
  • 343
  • This'll certainly work if I want the entire email to be in plain text, but more what I am looking for is just small portions of it to be code samples. The rest might have nice headings, bullet points, tables, etc. ... I write really LONG emails sometimes :-) – Mag Roader Jul 21 '09 at 04:58
  • 1
    This does not work if the entire email is plain text, because Outlook destroys the line break by inserting additional line breaks. – ceving Apr 02 '14 at 13:25
-5

Well this is late answer, but if you don't absolutely need it to be in text format, then why just simply copy/paste a screenshot taken with some tool like Windows build-in Snipping tool ?

vpalmu
  • 524
  • 4
  • 13