Inserting an image directly at the text cursor location in a Word document is not a one-click feature in the default ribbon. The standard Insert > Pictures menu places the image at the cursor, but it often shifts alignment or requires manual resizing and positioning. Using a VBA macro gives you precise control over where the image lands and how it behaves. This article explains how to write a VBA macro that inserts an image exactly at the cursor position and keeps it anchored to the surrounding text.
You will learn the core VBA objects such as InlineShape and Range, the differences between floating and inline images, and how to handle file selection. The guide includes a ready-to-use macro, explanations of each line, and tips to avoid common pitfalls like image distortion or wrong placement.
Key Takeaways: Inserting an Image at the Cursor with VBA
- Selection.Range.InlineShapes.AddPicture method: Inserts the image as an inline shape at the current selection point, keeping it tied to the text flow.
- FileDialog(msoFileDialogFilePicker): Lets the user choose an image file without leaving Word, avoiding hard-coded file paths.
- LockAspectRatio property: Prevents image distortion when you later resize the inserted image through code or manually.
Why Inserting Images at the Cursor in Word Requires VBA
The built-in Insert > Pictures command does place an image at the cursor, but it offers no programmatic control. You cannot batch-insert images, enforce a specific width, or guarantee the image stays inline with text. When you need to insert many images into a document with consistent formatting — for example, product photos in a catalog or screenshots in a manual — the manual method becomes slow and error-prone.
VBA macros solve this by using the InlineShapes collection. An inline shape behaves like a large text character. It sits on the same line as the surrounding text and moves when you edit the text. This is the most predictable way to insert an image at the cursor because the image becomes part of the paragraph. The macro can also lock the aspect ratio, set a default width, and even filter file types so that only images are selectable.
Before writing the macro, ensure your security settings allow macros to run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros. For production use, consider signing your macro with a digital certificate.
How to Write a VBA Macro to Insert an Image at the Cursor
The macro uses the FileDialog object to let the user pick an image. It then inserts that image as an inline shape at the current selection. The code below includes error handling for cases where the user cancels the dialog or selects a non-image file.
- Open the VBA Editor
Press Alt + F11 to open the Visual Basic Editor. In the Project pane, double-click Normal or the document name where you want the macro stored. Right-click the target module and choose Insert > Module to create a new code module. - Name the macro and declare variables
Copy and paste the following code into the module. The macro name isInsertImageAtCursor.Sub InsertImageAtCursor() Dim fd As FileDialog Dim filePath As String Dim img As InlineShape Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .Title = "Select an image to insert" .Filters.Clear .Filters.Add "Images", "jpg;jpeg;png;gif;bmp;tiff" .AllowMultiSelect = False If .Show = -1 Then filePath = .SelectedItems(1) Else MsgBox "No image selected.", vbExclamation, "Cancelled" Exit Sub End If End With Set img = Selection.Range.InlineShapes.AddPicture( _ FileName:=filePath, _ LinkToFile:=False, _ SaveWithDocument:=True) With img .LockAspectRatio = msoTrue .Width = InchesToPoints(3) End With Set fd = Nothing Set img = Nothing End Sub - Run the macro
Close the VBA Editor. Place your cursor at the exact location in the document where you want the image. Press Alt + F8, select InsertImageAtCursor, and click Run. A file picker dialog opens. Navigate to an image file and click Insert. - Verify the result
The image appears at the cursor as an inline shape. It is set to a width of 3 inches with the aspect ratio locked. You can change the width value in the macro by editing.Width = InchesToPoints(3)— replace 3 with any number. The macro saves the image inside the document so it does not depend on the original file location.
Alternative: Insert an Image Without a File Dialog
If you always insert the same image, you can hard-code the file path. Replace the file dialog block with filePath = "C:\Users\YourName\Pictures\logo.png". Be aware that hard-coded paths break if the image is moved or the macro runs on another computer.
Alternative: Insert as a Floating Shape
To insert the image as a floating shape that can be dragged freely, use Selection.Range.Shapes.AddPicture instead. Floating shapes are not anchored to the cursor in the same way — they attach to the nearest paragraph. For true cursor-position insertion, inline shapes are more reliable.
Common Mistakes When Inserting Images With VBA
The Image Appears at the Top of the Document Instead of the Cursor
This happens when you use ActiveDocument.InlineShapes.AddPicture without specifying a Range. The default range for a document-level call is the start of the document. Always use Selection.Range.InlineShapes.AddPicture to target the exact cursor position.
The Image Is Distorted or Cropped
Distortion occurs when you set both .Width and .Height without locking the aspect ratio. The macro above sets .LockAspectRatio = msoTrue and only changes width. The height adjusts automatically. If you must set both dimensions, calculate the ratio: .Height = .Width (originalHeight / originalWidth).
The File Dialog Shows No Images
The filter string in the macro includes common image extensions. If your image has an unusual extension like .webp, add it to the filter: "jpg;jpeg;png;gif;bmp;tiff;webp". You can also remove all filters to show every file: .Filters.Clear without adding any new filters.
The Macro Does Not Run Due to Security Settings
If Word blocks the macro, open the Trust Center and select Enable all macros. For a safer approach, save the document as a macro-enabled file (.docm) and digitally sign the macro. Unsigned macros from unknown sources are disabled by default.
Inline vs Floating Image Insertion: Comparison
| Item | Inline Shape (InlineShapes) | Floating Shape (Shapes) |
|---|---|---|
| Anchoring | At the exact cursor position as a text character | Anchored to the nearest paragraph, not the cursor |
| Text wrapping | Inline with text — no wrap setting | Default wrapping is Square or Tight — can be changed |
| Position stability | Moves with text edits like a character | Stays at a fixed page position unless you set relative positioning |
| VBA method | Selection.Range.InlineShapes.AddPicture | Selection.Range.Shapes.AddPicture |
| Best use case | Inserting images at a specific text location for reports or manuals | Inserting logos or graphics that must remain in a fixed page corner |
After inserting the image, you can convert an inline shape to a floating shape by right-clicking the image, selecting Wrap Text, and choosing a non-inline option. The reverse is not possible — floating shapes cannot be converted to inline shapes through the UI.
You can now insert any image at the exact cursor position using a VBA macro that preserves aspect ratio and file portability. To extend this macro, add a loop to insert multiple images from a folder by using Dir function calls. For advanced users, combine this macro with a user form that lets you pick the image width and file type before insertion.