How to Insert Source Code in MS Word

If you have ever tried to insert source code in MS Word, you know that it is a struggle. The formatting gets completely lost.

In most cases, copying and pasting programming code into a Microsoft Word document will not work.

In this tutorial, we will explore the proper way to embed your source code in MS Word. 


Why Does Source Code Behave Differently in Word?

If you directly paste programming source code into MS Word, the source code loses its formatting. This is because programming languages such as Python, Java, or HTML use different formatting approaches, which means different code indentation, text formatting, and symbols. 

Microsoft Word’s document structure, which is based on creating letters and documents, doesn’t support the different programming formatting styles.

Fortunately, we have three methods to make it able to insert programming code into a document without losing its formatting.

For this tutorial, we will use the following Python source code.

#Provide an input string and count the frequency of characters in that string
#The algorithm is 0(n) complexity time
 
temp_list = []
start = "a"
str = "ababcddefff"
 
def alpha_zeta():
    alpha = 'a'
    for i in range(0,26):
        temp_list.append(alpha)
        alpha = chr(ord(alpha) + 1)
    return temp_list
 
temp_list = alpha_zeta()
 
#print (temp_list)
 
def character_frequency(str, temp_list):
    for each in temp_list:
        freq = 0
        for i in str:
            if (i == each):
                freq = freq + 1
        print (each, freq)
 
character_frequency(str,temp_list)

#The algorithm is 0(n) complexity time

temp_list = []

start = “a”

str = “ababcddefff”

def alpha_zeta():

    alpha = ‘a’

    for i in range(0,26):

        temp_list.append(alpha)

        alpha = chr(ord(alpha) + 1)

    return temp_list

temp_list = alpha_zeta()

#print (temp_list)

def character_frequency(str, temp_list):

    for each in temp_list:

        freq = 0

        for i in str:

            if (i == each):

                freq = freq + 1

        print (each, freq)

character_frequency(str,temp_list)

4 Methods to insert source code into Microsoft Word

  • Use Second Document to embed source code
  • Use Notepad++ with Syntax Highlighting
  • Use Notepad++’s NppExport plugin
  • Use Special Paste Options

Method #1: Use Second Document to Embed Source Code

This method explains how to open a second document and paste the source code into it. 

Step #1: Open a document 

Open a new MS Word document or the existing document where you want to embed source code. 

Step #2: Insert a new object

In the top main menu bar, click on the Insert tab.

From the Insert ribbon, click on the Object icon in the Text group.

Tip: Hover with your mouse over the different icons to see their names.

Step #3: Create a new object type

Once you have clicked on Object, the Object dialog box will open.

Click on the Create New tab.

Under the heading Object type from the options shown, select Microsoft Word Document.

Make sure that the “Display as icon” checkbox is Unchecked.

Click on the OK button.

Step #4: Paste source code

A new document will open up with the name “Document in [Your Main Document].”

Right-click with your mouse on the blank document.

Select Paste from the menu that opens.

Or you can press Ctrl + V on your keyboard. (Command + V for Mac)

We will paste the Python programming code shown above.

Click on File in the main menu bar.

Click on Save as and give your document a name.

Save the document in the same folder as your main document.

Close the new document. 

Your source code will now appear in your main document, with the formatting intact.


Method #2: Use Notepad++ with Syntax Highlighting

Notepad++ has a nifty “Copy Text with Syntax Highlighting” feature, making this an easy method to use. Notepad++ is a popular text editor. It is free to use and you can download it from here: Notepad++ download.

Note: This application only works in an MS Windows environment.

Step #1: Open the source code file

Open the Notepad++ program.

Click on File in the main menu bar. 

A dropdown menu will open.

Click on Open.

The File Navigator will open. 

Navigate to where your source code file is located.

Select the source code file.

The source code will now be displayed in the Notepad++ window.

Step #2: Select code and choose the options

Select the source code by clicking and dragging with your mouse over everything, until all the code is highlighted.

Right-click with your mouse, while the pointer is on the highlighted section.

A menu will open.

Select Plugin commands.

A submenu will open.

Select Copy Text with Syntax Highlighting.

Step #3: Open MS Word document

Open a new MS Word document where you want to include your code. Or you can open an existing file.

Right-click with your mouse on the blank document.

Select Paste from the menu that opens.

Or you can press Ctrl + V on your keyboard. (Command + V for Mac)

The code, when embedded in MS Word, looks like this:


Method #3: Use Notepad++ NppExport Plugin

Notepad++’s NppExport plugin lets you keep source code formatting in MS Word. It is pre-installed with Notepad++ and can copy the source code format to the clipboard.

Note: This application only works in an MS Windows environment.

Step #1: Open the source code

Open the Notepad++ program.

Click on File in the main menu bar. 

A dropdown menu will open.

Click on Open.

The explorer will open. Navigate to where your source code file is located.

Select the source code file.

The source code will now be displayed in the Notepad++ window.

Step #2: Select NppExport From Plugins sub-menu

Click on Plugins in the main menu bar.

From the dropdown menu that opens, click on NppExport.

A submenu will open.

Select Copy all formats to clipboard.

Step #3: Open MS Word document

Open a new MS Word document where you want to include your code. Or you can open an existing file.

Right-click with your mouse on the blank document.

Select Paste from the menu that opens.

Or you can press Ctrl + V on your keyboard.  (Command + V for Mac)

It should look like our example below.


Method #4: Use Special Paste Options

The recent MS Word releases, including MS Word 2019 and Office 365, offer special paste options that let you keep your source code formatting. 

For example, you can keep formatting when pasting your source code from text editors such as Visual Studio Code, which we use here as an example.

Let’s see the steps to use special paste options with Visual Studio Code.

Step #1: Open source code file in Visual Studio Code

Open Visual Studio Code from your desktop.

Click on File in the main menu bar. 

From the drop down menu that opens, select Open File… 

The File Manager will open.

Navigate to where the file is you want to copy.

Click on its name to open it in the application.

Step #2: Select the code and copy it

Select the code of the file by clicking and dragging over it with your mouse.

Right-click with your mouse and select Copy from the menu that opens.

You can also copy the code using Ctrl + C on Windows or Command + C on Mac.

Step #3: Open MS Word document

Open a new MS Word document where you want to include your code. Or you can open an existing file.

Right-click with your mouse on the blank document.

Select Paste from the menu that opens.

Or you can press Ctrl + V on your keyboard. (Command + V for Mac)

Be sure to select the first Paste option, which is “Keep Source Formatting(K).”

Here is how it shows in Microsoft Word.


Conclusion

Now you know that it is possible to insert Source Code into MS Word without losing the formatting.

Although Methods 2 and 3 can only work in an MS Windows environment, users of other operating systems can try out Methods 1 and 4.

Between these four options, you simplify working with source code in Word and improve productivity and document shareability.

Leave a Comment