When you insert an image into a Word document, the program automatically generates alternative text based on the file metadata or the image content analysis. This auto-generated alt text is often generic or inaccurate, and it never includes the original filename of the picture. You want Word to read the picture’s filename and use that as the default alt text instead. This article explains how to configure Word to automatically populate the alt text field with the filename of each inserted image using a simple VBA macro. You will learn how to add the macro, run it, and adjust the behavior for future documents.
Key Takeaways: Set Word to Auto-Read Picture Filename as Alt Text
- VBA macro in Normal.dotm: A macro that loops through all pictures in a document and sets the alt text to the image’s source filename.
- AutoOpen or Document_Open event: Run the macro automatically each time you open a document, ensuring new images get the filename alt text.
- File > Options > Trust Center > Macro Settings: Enable all macros or digitally sign the macro to avoid security warnings on every run.
How Word Generates Default Alt Text for Pictures
Word uses an online service called Office Intelligent Services to analyze the content of an inserted image and generate descriptive alt text. This feature is enabled by default in Word for Microsoft 365. The service attempts to identify objects, people, text, and scenes within the picture. For many images, the resulting alt text is vague, such as “A picture containing text, screenshot” or “A close-up of a person.” The original filename of the image file is never included in this auto-generated description.
The filename often contains meaningful information that the user intentionally assigned, such as “2024-Annual-Report-Cover.png” or “Product-Shot-Back-View.jpg.” Using the filename as the alt text gives screen reader users immediate context about the image’s purpose without needing to interpret a generic description. Word does not offer a built-in setting to change the default alt text source from the intelligent service to the filename. You must use a VBA macro to intercept the insertion event or to process existing images.
Prerequisites for the Macro Solution
Before you start, ensure the following conditions are met. You need a copy of Word for Microsoft 365 or Word 2019 or later on Windows. The macro will be stored in the Normal.dotm template so it applies to all documents. You must have macro security set to allow signed macros or to enable all macros temporarily. If your organization blocks macros, contact your IT department. The macro works only with images that have a source file path. Images copied from the clipboard or pasted as a screenshot do not have a filename and are skipped.
Steps to Add the VBA Macro That Sets Alt Text From Filename
- Open the Visual Basic for Applications editor
Press Alt+F11 in Word. The VBA editor window opens. In the left pane, locate Project (Normal). Expand it and double-click ThisDocument under Microsoft Word Objects. - Paste the macro code into the code window
Copy the following VBA code and paste it into the blank area of the code window. This macro loops through all inline shapes and shapes in the document. For each picture, it extracts the source filename from the LinkFormat.SourceFullName property and writes it into the AlternativeText property.Sub SetAltTextFromFilename()
Dim pic As InlineShape
Dim shp As Shape
Dim fileName As String
Dim altText As StringFor Each pic In ActiveDocument.InlineShapes
If pic.Type = wdInlineShapePicture Then
If pic.LinkFormat Is Nothing Then
' Picture is embedded, not linked — skip
Else
fileName = pic.LinkFormat.SourceFullName
altText = Mid(fileName, InStrRev(fileName, "\") + 1)
pic.AlternativeText = altText
End If
End If
Next picFor Each shp In ActiveDocument.Shapes
If shp.Type = msoPicture Then
If shp.LinkFormat Is Nothing Then
' Picture is embedded, not linked — skip
Else
fileName = shp.LinkFormat.SourceFullName
altText = Mid(fileName, InStrRev(fileName, "\") + 1)
shp.AlternativeText = altText
End If
End If
Next shp
End Sub - Close the VBA editor and save the template
Press Ctrl+S to save the Normal.dotm template. Close the VBA editor window. Word asks for confirmation to save changes to Normal.dotm. Click Save. - Run the macro for the first time
Open a document that contains pictures with filenames. Press Alt+F8 to open the Macros dialog. Select SetAltTextFromFilename from the list and click Run. Word processes each picture and sets the alt text to the filename. Check the alt text by right-clicking a picture and choosing View Alt Text. - Assign the macro to a button or keyboard shortcut for quick access
Go to File > Options > Quick Access Toolbar. In the Choose commands from dropdown, select Macros. Select SetAltTextFromFilename and click Add. Click Modify, choose a button icon, and rename it to something like “Alt Text from Name.” Click OK twice. Now you can run the macro with one click from the Quick Access Toolbar.
Making the Macro Run Automatically on Document Open
To apply the filename alt text to every new picture you insert, you can attach the macro to the Document_Open event. In the VBA editor, in the ThisDocument code window, paste the following code above the existing macro:
Private Sub Document_Open()
Call SetAltTextFromFilename
End Sub
Save the template. Now every time you open any document, the macro runs and updates the alt text for all pictures. Note that this also runs on documents that contain no pictures, which is harmless but adds a slight delay.
Limitations and Things to Avoid When Using Filename as Alt Text
Pictures Pasted From Clipboard or Screenshot
Images that are pasted directly from the clipboard or inserted as a screenshot do not have a source file path. The LinkFormat.SourceFullName property is empty for these images. The macro skips them, leaving their alt text unchanged. To set alt text for these images, you must manually type a description in the Alt Text pane.
Embedded Pictures Without a Link
When you insert a picture using Insert > Pictures > This Device, Word embeds the image file into the document. The macro checks for a LinkFormat object. If the picture is embedded, LinkFormat is set to Nothing, and the macro skips the picture. This is a known limitation of the VBA object model. To work around this, insert images as linked files: use Insert > Pictures > Link to File instead of embedding. Linked pictures retain the source file path.
Macro Security Warnings
Word may show a security warning bar stating that macros have been disabled. Click Enable Content to allow the macro to run. To avoid this prompt, you can digitally sign the macro using a self-signed certificate or add the document’s location to the Trusted Locations list in File > Options > Trust Center > Trust Center Settings > Trusted Locations.
Alt Text Overwrites Existing Descriptions
The macro replaces any existing alt text with the filename. If you have already written meaningful alt text for some images, the macro will overwrite it. Run the macro only on documents where you want the filename to be the alt text. Alternatively, modify the macro to check if the alt text is empty before writing the filename.
| Item | Embedded Picture | Linked Picture |
|---|---|---|
| Source file path retained | No | Yes |
| Macro sets alt text from filename | No (skipped) | Yes |
| File size in document | Large (image embedded) | Small (path only) |
| Image moves if source file is deleted or renamed | No | Yes (broken link) |
Now you can configure Word to automatically set the alt text of inserted pictures to their original filename using a VBA macro. Test the macro on a sample document first. For a more refined solution, modify the macro to strip the file extension or to append a prefix. Store the macro in Normal.dotm so it is available in every document you create.