Iraj
Iraj

Reputation: 389

Copy the code text from MS Visual Studio and keeping the source color and formatting

I used to copy the text of my C# code in a word document to have a backup of my progress. Previously the color of the codes (blue, black, green) was kept when I paste it in any text editor program (MS word, WordPad ...). Recently it is not so. The format of the text (indentations) is kept but the color unfortunately is just black. That helped me a lot to have previous version of my code on another monitor. The codes are displayed just normal in Microsoft Visual Studio but the color is lost when I copy and paste it anywhere else. (I have checked the paste setting of MS word and they are set to keep the formatting of the source. The problem shouldn't be there.)

I use Microsoft Visual Studio Professional 2019.


Edit and answer:

The problem was that although the "cope rich text on copy/cut" option was checked, my code exceeded the maximum character count for this property. It was solved by changing the "Max Length" to a very big number. Default is 10240 and I added three more zeros.

Therefore the path to the solution is:

Tools --> options --> Text Editor --> Advanced --> Copy rich text on copy/cut (checked) --> Max Length (a very big number)

enter image description here

Upvotes: 4

Views: 7162

Answers (4)

Thomas Koelle
Thomas Koelle

Reputation: 3742

Nothing worked for me (Visual Studio 2022), but if I open the files in VS-code then they copy/paste to Word with colours.

So that was my solution.

Upvotes: 0

Winston L
Winston L

Reputation: 61

Some months back the default action of MSVS is to copy the text with syntax formatting so I didn't have a problem but was a bit miffed when only plain text is copied when I copy too much.

I think there was a recent update where I discovered that only plain text is being copied.

I tried many extensions (Copy as HTML, Syntax Highlighting Pack, Copy with Line Number) to no avail. Google search could not give me the answer.

Then I found by typing "copy" into the MSVS search bar that there is this new option :

Text Editor -> Advanced -> Copy rich text on copy/cut :

Max length : 10240

? Use accurate classification

I have no idea what Use accurate classification means.

Maybe the option was there all along just that I didn't know about it.

Only by google search of "Use accurate classification" could I find this post.

Still no description of what "Use accurate classification" is about.

Anyway happy that it is finally working now.

Upvotes: 0

joakimriedel
joakimriedel

Reputation: 1959

You need to check the options "Copy rich text on copy/cut" and "Use accurate classification" in the Text Editor->Advanced settings dialogue within Visual Studio 2019.

Sample screenshot from settings

Upvotes: 4

Jamie.G099
Jamie.G099

Reputation: 11

If you download Notepad++ you can use that as a backup medium.

Copy and paste your code into Notepad++, select "Language" at the top, hover over "C" and select "C#".

Once this is done, depending on how your Notepad is configured you should have the key functions etc in a different colour.

If you don't, do the following:

Settings --> Style Configurator --> Click on C# --> Click "Default" --> and set "Foreground Colour" to a colour of your choice. In my example I have chosen blue, which will look something like this:

enter image description here

Upvotes: 1

Related Questions