Word VBA Macro to Export Images and Replace with File Names
Question details
The user needs a VBA macro to automatically extract images from a Word document to a local folder and replace the original images in the document with their newly generated file names.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Extracting embedded images from a document for external use while maintaining text placeholders in the exact locations where the images originally appeared.
- Observed behavior
- Embedded Office images lose their original file names, requiring a custom macro to generate new file names, export the images, and handle replacement for both inline and floating shapes.
Always create a backup copy of your Word document before running VBA macros that modify, replace, or delete content to prevent accidental data loss.
Create a VBA Macro to Export and Replace Shapes
Use a custom VBA script to iterate through the document's shapes, save the images to a local directory, and insert text placeholders.
Because embedded images in Office files no longer retain their original metadata, your macro will need to dynamically generate file names (e.g., Image_001.jpg) during the export process.
The script must handle two distinct shape types: inline shapes, which act like text and can be directly replaced with a text string, and floating shapes, which are anchored and may require inserting a text box to hold the generated file name.
Press Alt + F11 on your keyboard while your Word document is open to launch the Microsoft Visual Basic for Applications window.
Click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your code.
Write a 'For Each' loop to iterate through 'ActiveDocument.InlineShapes'. Within the loop, use an extraction method to save the image to your target folder, then use 'Selection.TypeText' to insert the generated file name text before deleting the shape.
Write a second 'For Each' loop for 'ActiveDocument.Shapes'. After saving the floating image, create a new text box at the shape's original anchor location, insert the file name text into it, and delete the original shape.
Press F5 or click the Run button to execute the script on your document. Review the designated output folder to ensure all images were exported.

Try WPS Office for Seamless Macro and Document Management
If you frequently work with automated tasks and macros in document processing, consider switching to WPS Office. It provides robust compatibility with Microsoft Word formats and supports VBA scripting in a lightweight, user-friendly environment.
- 1. Download WPS Office: Visit the official WPS website to download the free, lightweight installer.
- 2. Install and Launch: Run the installer and open the WPS Writer application.
- 3. Enable VBA Support: Access the Developer tab to seamlessly run your existing Word VBA macros in WPS Office.

Frequently Asked Questions
Why do embedded images in Word lose their original file names?
When images are pasted or embedded into Office documents, they are converted into internal Office shape objects. This process strips their original metadata and file paths to optimize file size and prevent broken external links.
What is the difference between inline and floating shapes in Word VBA?
Inline shapes act like text characters within a paragraph and move fluidly with the text flow. Floating shapes are anchored to a specific paragraph or page element but can be freely moved around the page independently of the text flow.
Can I extract images from a Word document without using a VBA macro?
Yes, you can extract all images at once by saving the document as a Web Page (HTML) format, or by changing the '.docx' file extension to '.zip' and extracting the 'media' folder inside. However, these methods will simply extract the images and will not replace them with text placeholders in your document.




