Replace a Picture in a DOCX File Using Word VBA
Question details
Replace an existing picture in a DOCX document with a generated EMF image using Word VBA, preferably without saving the EMF to a file.
- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A VBA automation process places a generated EMF on the clipboard and needs to replace an existing document picture.
- Observed behavior
- Pasting over the existing picture inserts another image in DOCX rather than replacing it. Copying an EMF from a DOCX document may also produce bitmap operations that affect resizing.
Identify whether the target picture is inline or floating; the VBA example below is for an inline picture. Place the generated image on the clipboard as an enhanced metafile before running the macro.
Replace an inline picture with a clipboard EMF using VBA
Delete the target inline picture, paste the clipboard EMF at its position, and restore its dimensions.
This approach avoids saving the generated EMF to a file. It requires the clipboard to contain an enhanced metafile and does not preserve floating-picture wrapping or positioning.
In Word, open the DOCX document, press Alt+F11, and choose Insert > Module.
Paste this code into the module. It targets the first inline picture in the active document; change the index to match your target. Sub ReplaceInlinePictureWithEMF() Dim doc As Document Dim pic As InlineShape Dim r As Range Dim pos As Long Dim picWidth As Single, picHeight As Single Set doc = ActiveDocument Set pic = doc.InlineShapes(1) pos = pic.Range.Start picWidth = pic.Width picHeight = pic.Height pic.Delete Set r = doc.Range(Start:=pos, End:=pos) r.PasteSpecial DataType:=wdPasteEnhancedMetafile If r.InlineShapes.Count > 0 Then With r.InlineShapes(1) .LockAspectRatio = False .Width = picWidth .Height = picHeight End With Else MsgBox "No inline picture was found in the pasted range." End If End Sub
Copy the generated EMF to the clipboard, return to the VBA editor, and run ReplaceInlinePictureWithEMF. In the document, check the image’s size and quality, including after resizing.
Replace a picture using a temporary image file
Use a file-based replacement when the clipboard does not provide a usable EMF or when you can save the generated image temporarily.
Edit DOCX Pictures with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for editing documents and pictures. Open compatible Word files to move between Office applications, while using Microsoft Word when your workflow depends on Word-specific VBA behavior.

Frequently Asked Questions
Why does pasting over a picture add another image in DOCX?
The paste operation may insert clipboard content rather than replace the existing picture. For an inline picture, delete the target and paste an enhanced metafile at its former range.
Can I replace an inline picture without saving the EMF?
Yes. Copy the generated EMF to the clipboard, delete the target inline picture in VBA, and use Range.PasteSpecial with DataType:=wdPasteEnhancedMetafile.
How can I keep the original picture size?
Before deleting the inline picture, record its Width and Height. After pasting, apply those values to the new InlineShape and check the result in the document.
Will the inline-picture macro preserve text wrapping?
No. The example is for inline pictures. A floating picture requires separate handling of its anchor, wrapping, and position; check Layout Options after replacement.
Can I use Publisher.PictureFormat.Replace in Word VBA?
That method is documented for Publisher, not as a general Word picture replacement method. In Word, use an appropriate Word object-model approach, such as deleting and reinserting the picture.




