Inserting Pictures in MS Word Using Codes A Step-by-Step Guide

How to input pictures in ms Word through codes

Microsoft Word is a powerful word processing tool that allows users to create and format documents with ease. While most users insert pictures using the built-in graphical interface, it is also possible to insert images programmatically using codes. This can be particularly useful for automating document generation, creating templates, or when dealing with a large number of images. In this article, we will explore how to insert pictures in MS Word through codes, providing a step-by-step guide for both beginners and experienced users.

Why Insert Pictures Using Codes?

Before we dive into the technical details, let’s understand why one might want to insert pictures in MS Word using codes. There are several advantages to doing so

Automation If you need to generate documents with a large number of images, manually inserting each one can be time-consuming. Using codes allows you to automate this process, saving you valuable time.

Consistency Codes ensure that images are inserted consistently in the desired location, size, and format, reducing the chances of human error.

Dynamic Documents With code, you can create dynamic documents that pull images from a database, online sources, or user input.

Customization Codes give you greater control over the formatting and placement of images, allowing you to tailor documents to your specific needs.

Inserting Pictures with VBA (Visual Basic for Applications)

Microsoft Word supports Visual Basic for Applications (VBA), a powerful scripting language that enables automation and customization of Word documents. Here’s how you can insert pictures using VBA

  • Enable Developer Tab First, you need to enable the Developer tab in Word. Go to “File” > “Options” > “Customize Ribbon,” and check the “Developer” option.
  • Open Visual Basic for Applications (VBA) Editor Click on the “Developer” tab, then select “Visual Basic” to open the VBA editor.
  • Insert a Module In the VBA editor, click on “Insert” and choose “Module” to insert a new code module.
  • Write the Code You can now write the VBA code to insert the picture. Here’s a simple example


Sub InsertPicture()

    Dim rng As Range

    Set rng = ActiveDocument.Range

    ‘ Insert the picture from a file

    rng.InlineShapes.AddPicture “C:\Path\to\YourImage.jpg”, LinkToFile:=False, SaveWithDocument:=True

End Sub


This code inserts the image located at “C:\Path\to\YourImage.jpg” into the document.

Run the Code Close the VBA editor and run the code by clicking on the macro in the Developer tab or pressing “Alt” + “F8.”

Inserting Pictures with Python and the `docx` Library

If you prefer working with Python, you can use the `docx` library to programmatically create and modify Word documents. Here’s how to insert pictures using Python

Install the `python-docx` Library If you haven’t already, install the `python-docx` library using pip:


pip install python-docx


Write the Python Code


from docx import Document

from docx.shared import Inches

# Create a new Word document

doc = Document()

# Insert a picture

doc.add_picture(‘path_to_image.jpg’, width=Inches(4), height=Inches(3))

# Save the document‘my_document.docx’)


In this code, we first create a new Word document, insert a picture from the specified path, and save the document.

Run the Code Execute the Python script, and it will generate a Word document with the picture inserted.

Inserting Pictures with XML

If you prefer a more manual approach, you can also insert pictures in Word documents using XML code. However, this method is less common and may require a deeper understanding of the Word document structure. Here’s a simplified example




        <v:shape id=”_x0000_i1025″ type=”#_x0000_t75″ style=”width:200px;height:150px”>

            <v:imagedata src=”file:///C:/Path/to/YourImage.jpg” r:id=”rId1″/>





This XML code represents the insertion of an image, but you’ll need to integrate it into your Word document’s XML structure. Be cautious when editing Word documents directly in XML, as it can be complex and error-prone.

Frequently Asked Questions

What is the shortcut to insert a picture in Word?

In your Word document, place the insertion point where you want to insert an image. Press Alt+N, P, D. The Insert Picture dialog box opens.

How do I insert a Picture in Word without losing quality?

Step 1: In Word, go to File, click on Options > Advanced. Step 2: Under Image Size and Quality, select High fidelity in the Default resolution list. Step 3: Select the Do not compress images in the file check box.


Inserting pictures in MS Word through codes provides a valuable means of automation, customization, and dynamic document creation. Whether you choose VBA, Python with `python-docx`, or XML, the method you select should align with your expertise and project requirements. By mastering these techniques, you can harness the full power of Microsoft Word for your document creation needs. Remember to save your work and make backups when working with automated document generation to prevent accidental data loss or corruption. Happy coding!

Read Also : Becoming a Robot Technician A Guide to Unlocking the Future