How do you display code snippets in MS Word preserving format and syntax highlighting?

Anonymous
2023-11-04T12:06:08+00:00

Does anyone know a way to display code in Microsoft Word documents that preserves coloring and formatting? Preferably, the method would also be unobtrusive and easy to update.

I have tried to include code as regular text which looks awful and gets in the way when editing regular text. I have also tried inserting objects, a WordPad document and Text Box, into the document then putting the code inside those objects. The code looks much better and is easier to avoid while editing the rest of the text. However, these objects can only span one page which makes editing a nightmare when several pages of code need to be added.

Lastly, I know that there are much better editors/formats that have no problem handling this but I am stuck working with MS word.

Microsoft 365 and Office | Word | Other | Other

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} votes

4 answers

Sort by: Most helpful
  1. Charles Kenyon 159.1K Reputation points Volunteer Moderator
    2023-11-05T16:24:51+00:00

    Not preserving the color coding, I use a custom character style which is mono-spaced and set to not check spelling and grammar.

    When I need to preserve more, I use an Add-In.

    .

    9 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-11-05T00:59:50+00:00

    Hi ALEx9ci

    Thank you for posting on the Microsoft Community forum.

    I understand that you are looking for a way to display code snippets in Microsoft Word documents that preserves formatting and syntax highlighting. I can only imagine the inconvenience dealing with this issue, especially when you are working with a large amount of code. Let's work together to find the best solution to this problem.

    The option that you can try is to use the “Code Format” add-in, which is available for Office 365 and later versions. This add-in allows you to select all code (even multiple pages) and convert it into a formatted color code with line numbers. To install add-ins, you can go to Insert tab > then click Add-ins > search for the add-in and follow the on screen instructions.

    If you prefer not to use add-ins, you can use styles or alternatively images. Styles are meant for formatting. You can export RTF from Notepad++, then use styles to format the code in Word. Alternatively, you can use images to display the code. You can take a screenshot of the code and insert it into the document. This method is not ideal as it is not searchable, but it is an option if you are unable to use add-ins or styles.

    I hope these solutions help you display your code snippets in Microsoft Word documents with ease.

    Sincerely,


    ReynaldoB

    Microsoft Community Forum Moderator

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2023-11-06T06:48:21+00:00

    If you want to insert the code or command in your document only for the reader to see and not copy or edit, you can insert an object containing the snippet. Go to the Insert tab, click the Object drop-down arrow, and pick "Object." On the Create New tab, select "OpenDocument Text" as the Object Type. Click "OK."

    6 people found this answer helpful.
    0 comments No comments
  4. Anonymous
    2023-11-09T05:57:34+00:00

    Hi ALEx9ci,

    Thank you for sharing this information. This would be very helpful to other community members who would have the same concerns.

    We really appreciate your time.

    Sincerely,


    ReynaldoB

    Microsoft Community Forum Moderator

    0 comments No comments