none
Word Formatting with C# RRS feed

  • Question

  • Does anyone know a way to display code in Microsoft Word documents that preserve colors and formatting?

    I have tried to include code in the form of ordinary text that looks awful and obstructs normal text editing. I also tried inserting objects, a Wordman document and Text Box, into the document then placing the code inside those objects. The code looks much better and easier to avoid while editing the rest of the text. However, these objects can only expand one page, making editing a nightmare when adding some code pages.

    At the same time, I also want to install a method or a program specifically for Word using C #. Any suggestions?

    Thursday, August 1, 2019 2:57 AM

Answers

  • For me, this is the best way to add code inside word:

     Insert -> Text -> object(on the right side) -> choose OpenDocument Text -> copy and paste your code inside this embedded word page -> Save

    The result looks very nice. Here are the advantages of this method:

    • the original layout and colors of the code are keeped.
    • the code is separated from the rest of the document.
    • spelling errors won't be highlighted in the code.

    Finally, if you want to create a Code using C#,  you may want to visit this: https://docs.microsoft.com/en-us/office/troubleshoot/office-developer/automate-word-create-file-using-visual-c
    Or, another way is using a third-party library like Ultimate Word Library and follow their example to create your own.

    Good luck !

    • Marked as answer by Vo Hoang Ngoi Thursday, August 1, 2019 3:06 AM
    Thursday, August 1, 2019 3:05 AM