245

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?

1
  • 3
    "Developers, developers, developers, developers" - 14 years have passed and this is still not a plug-in, let alone a standard features in outlook...
    – Exodus
    Feb 8 at 21:28

12 Answers 12

180

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.

3
  • 10
    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, 2014 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, 2022 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 3, 2022 at 17:08
174

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

8
  • 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 8, 2017 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, 2018 at 10:31
  • Thanks, is a bit of pain if you have npp 64bit, no plugin manager, some fixes here - github.com/notepad-plus-plus/notepad-plus-plus/issues/…
    – cardamom
    Nov 5, 2018 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 5, 2019 at 18:14
  • 4
    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, 2019 at 10:32
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
9
  • 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, 2014 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, 2015 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? Oct 1, 2015 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, 2017 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 May 5, 2019 at 7:18
30

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.

2
  • 1
    That's exactly what I use: VSC mantains its own formatting in all MS softwares
    – Brigo
    Sep 26, 2020 at 13:05
  • This answer should have more upvotes! Exactly what I use as well.
    – rasengan__
    Feb 29 at 13:44
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

2
  • 7
    You have to install SublimeHighlights plugin for this to work
    – kendotwill
    Mar 31, 2016 at 21:47
  • 1
    Worked with SublimeHighlights. I use sublime and I'm so relieved that I can copy paste code into email without an additional step. Aug 10, 2016 at 9:25
11

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

8

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.

1
  • 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 <code> 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, 2009 at 15:35
7

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.
2
  • 1
    Wow, oddly enough this worked on my Mac! Oct 3, 2022 at 15:19
  • I tried this and it put in a white background. Both my VS Code and Outlook 365 web are using dark mode. Any way to edit to formatting code? Oct 12, 2023 at 14:15
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.

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).

2
  • 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, 2009 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, 2009 at 3: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

2
  • 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, 2009 at 4: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 2, 2014 at 13:25
-7

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 ?

1
  • 7
    “My code needs to be copyable out of the email and directly into code”
    – Mag Roader
    Jan 22, 2022 at 12:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.