57

Is there a way to copy code from visual studio (C#) and paste it into OneNote, without losing the formatting?

I was able to do this, but only if I copy from VS, paste to Word, copy from Word, and then paste to OneNote.

CJBS
  • 15,147
  • 6
  • 86
  • 135
MedicineMan
  • 15,008
  • 32
  • 101
  • 146

13 Answers13

43

This is an option that seems to be disabled by default.

To enable (in VS 2019):

  1. Go to Tools -> Options

  2. type copy in the search box

  3. Under Text Editor -> Advanced...

  4. Check Copy rich text on copy/cut

Visual Studio Options Example

Top copy as formatted

Once the feature is enabled, depending on the target, this may be a two-step process. If, after copying and pasting code, it still appears as unformatted (e.g. if pasting into a web browser), use the approach suggested by marcus, by first pasting into wordpad.exe (start -> run -> type wordpad), then copying the text from within Wordpad again, and pasting into the target application/browser.

CJBS
  • 15,147
  • 6
  • 86
  • 135
38

There is fortunately a solution for Visual Studio 2010! Install the Visual Studio 2010 Pro Power Tools extension and copy/paste to OneNote retains syntax highlighting.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Jan Roelof
  • 531
  • 5
  • 3
  • 1
    Per @Tom A below, check out Microsoft's Productivity Power Tools 2015 for those of us using a slightly more recent VS. – JOpuckman Feb 22 '17 at 19:20
  • 1
    Or Productivity Power Tools 2017 for those of us using an even more recent VS. – DaveTheMinion Aug 25 '17 at 00:23
  • 3
    You will not be surprised to find out that its available for VS 2019 as well, 10 years later – Olegp Aug 12 '20 at 05:49
  • And also for VS 2022: https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.ProductivityPowerPack2022 – Yogi Nov 27 '21 at 15:18
22

You have hit the nail on the head on the process. The application that you are pasting to must be able to accept the code with the formatting properly. MS Word is one of the few that handles this properly, therefore, going from VS -> Word -> OneNote will work. But VS -> OneNote doesn't work.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • 3
    another app that will work are a message in outlook (best with it set to Rich Text Mode) – ShuggyCoUk Apr 23 '09 at 19:55
  • @ShuggyCoUk - Yes, that is correct, but I believe that it only works if you have Word as the editor. – Mitchel Sellers Apr 23 '09 at 21:31
  • 1
    I tried the trick, but it didn't work fully for me. Colors were correct but the indention was lost. – Karsten Mar 11 '10 at 10:56
  • 1
    This is an old answer, so for clarity this still works nicely for VS2015 and Office 2015. Notably if you have to much indentation, simply hold `alt` when selecting in Visual Studio. – Alex KeySmith May 31 '16 at 15:40
4

NoteHighlight will do the trick. I found it through the following blog post, codefoster.

WerWet
  • 323
  • 5
  • 14
charlie hwang
  • 454
  • 4
  • 8
4

There is an add-in for Microsoft Visual Studio that allows you to copy source code, syntax highlighting, and line numbers as HTML.

With this add-in, you can copy from VS and paste HTML directly to your target.

Check it out here: CopySourceAsHtml

Lawrence P. Kelley
  • 4,276
  • 3
  • 28
  • 24
2

The best way of keeping the format of your code completely is constituted from two steps:

  1. In the first step right click the code and click "copy" then paste it in "wordPad"
  2. In the second step copy from "WordPad" to word.

Copying Code Pasting Code

CJBS
  • 15,147
  • 6
  • 86
  • 135
marcus
  • 21
  • 1
2

Try this elegant solution directly from Microsoft: Productivity Power Tools 2015

https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ProductivityPowerTools2015

The HTML copy-paste works nicely with OneNote.

Tom A
  • 595
  • 7
  • 16
2

Well , sorry for replying late, I know it's very old post. But , it might help to one who is trying to find it out solution for above said question. I found none to work for me even OneNote 2016, but here it is very easy way to do.

Just copy your code and paste it to wordpad.exe it suppose to paste with formatting and colors. Save *.rtf file and open it in Ms Word , from there you can copy/paste any document in word, it worked for me.

qbaloch
  • 43
  • 8
1

The GEM OneNote addon has a syntax highlighting feature -> http://www.onenotegem.com/blog/onenote-insert-highlight-syntax-source-code

Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32
1

Notehighlight supports syntax highlighting for : C#, SQL, CSS, JS, HTML, XML, JAVA, PHP,Perl, Python, Ruby, C++

They have release a port has been release compatible with OneNote 2013 (Also happens to work with OneNote2016)

Des Horsley
  • 1,858
  • 20
  • 43
0

I just figured out a workaround to do this without any plugins.

  1. Insert a table to OneNote with only one cell
  2. Change the shading color of this table to vscode background color, you can use the color picker to pick the bg color of vscode
  3. Choose the code block you want to copy and convert the indent to tab(otherwise the indent will be lost when pasting) reference: https://github.com/Microsoft/vscode/issues/35954
  4. Copy the selected code and paste it into the cell
  5. Change back the code indent of source code to space.

PS: I have tried in macOS it works fine

screenshot image

Y.Wang
  • 13
  • 4
0

I just view the code on the website of my repository of choice and copy from there. Then paste to OneNote with keep source formatting.

John
  • 1,714
  • 21
  • 41