How to include a source code snippet in Word?

Anonymous
2023-05-20T16:13:17+00:00

Sometimes, when writing a text in Word we want to mention part of the source code literally, either because we are writing a step-by-step manual, or to give an example. Over time, I have occasionally read on the Internet some articles that do it in an enviable way, but I have not been able to imitate it.

Currently, I copy from Notepad++ using the option to preserve sitaxis highlighting and it works, but I would like to take advantage of some more benefits, which I understand are possible in Word. I am also not completely satisfied with the option to insert an object into the document, of type "opendocument", because I do not find it advantages over how I currently do it.

What I would like to improve is:

  1. Wrap all the code in one object, so that it is easy to manipulate the styling of all the inserts throughout the text.
  2. The object should be preceded by a label identifying the programming language used, and if possible, a few lines clearly indicating the beginning and end of the code.
  3. The object shall be isolated, so that it does not affect the rest of the document, generating later problems of incompatibility, corruption, or automatic formatting changes.

I have researched in the community, searched in search engines, and tried the advice of numerous proposals without success. I hope that someone knows an effective formula, or at least, that we learn something from this discussion. Thanks for participating, best regards to all.

Microsoft 365 and Office | Word | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Jay Freedman 205.8K Reputation points Volunteer Moderator
    2023-05-20T18:40:22+00:00

    If you want to preserve the syntax coloring/highlighting that appears in whatever code editor you use, I suggest that you use a screenshot application to capture the snippets from the editor as graphical files (such as *.jpg or *.png) that you can insert into Word documents. Inserting RTF text into Word, while it does preserve the syntax formatting, fails the other requirements of a single object per snippet and isolation from the rest of the document.

    I'm familiar with Snagit from TechSmith, which easily scrolls text to capture multiple-screen objects. It also has tools for adding callouts, cropping and splitting images, and other useful operations. Other applications may have similar features.

    If you expect to edit the captured text, you can't do that easily with a graphical image. Instead, you should make a copy of the original code file, edit it, and then take a screen shot.

    For the labeling of a snippet, once it's inserted as a picture, you can use Word's built-in Insert Caption dialog to enter the label text. If necessary, the font and/or paragraph format of the style named "Caption" can be modified.

    3 people found this answer helpful.
    0 comments No comments
  2. Charles Kenyon 158.6K Reputation points Volunteer Moderator
    2023-05-20T18:56:59+00:00

    I also use Snagit from TechSmith.

    When I want the code in Word text, I use a Code paragraph style which is set to be Courier New 9 pt. and set to not check spelling or grammar.

    Where I want color coding and the like, for bigger projects, I use Easy Code Formatter a free add-in.

    3 people found this answer helpful.
    0 comments No comments
  3. Suzanne S Barnhill 274.6K Reputation points MVP Volunteer Moderator
    2023-05-21T01:38:19+00:00

    Word has a built-in Plain Text style that is 10-point Courier New. The HTML Code style is the same.

    3 people found this answer helpful.
    0 comments No comments