h1

Word: Extract Alt Text from images

August 29, 2018

Mary, one of this blog’s regular readers and donors, emailed me with a problem:

I want to manage all the image files in my Word 2007 docs and RoboHelp online help projects. Many times the image from the Word doc is used in the online help. Also an image can be used in more than one doc. So when an image is edited/updated it would be useful to know where it has been used. For that I need to know the image file name. Is there any way to find the original file name of the image? I know the ‘trick’ of saving as a web page to produce a media folder but the images are (unhelpfully) renamed as 1, 2, 3 and so on.

Hmmm… A curly one, for sure. Like Mary, I’d found that the images saved when you save to XML or HTML were unhelpfully named. I even checked the image properties to find if any of the information about the original image is preserved in the ‘save as’ file—it isn’t. So I went back to my test document in Word to look at the image properties and discovered something I hadn’t noticed before. In my test document, there was an Alt Text description for some of the images, AND, helpfully, that description often included the file name and sometimes the full file path on the network (see the follow-up post to this one for information on the method to use to always get the file name/path added automatically to the Alt Text description). This was a big bonus! Knowing that this information is stored with the image in the Word document means there has to be a way to extract it, right?

Well, yes there is, but all extraction methods below assume that the images have an Alt Text description associated with them. If you don’t have any Alt Text, there’s nothing to extract. For new documents, I would suggest that you start adding Alt Text to your images from now on (good practice for accessibility reasons anyway), and if you ever need the name of the original file, put that in the Alt Text description (along with the file path, if that’s unlikely to change). You can do this manually, or, if you use the right method, Word can add it automatically for you. For older documents, you’d have to find the original file and then add the Alt Text—this might not be practical, especially if you have a lot of documents with a lot of images. For information on how to add Alt Text, see: https://www.itsupportguides.com/knowledge-base/office-2016/word-2016-how-to-add-alt-text-to-images/

Now as to the solutions I looked at and that worked, from low tech to high tech. These solutions range from manually finding the Alt Text description (in the HTML file), to running a macro to display the Alt Text in a message box for each image, to running a macro that extracts all the Alt Text from a selected file and puts it into a new Word document. Choose the one that most suits your purposes.

I must thank John SR Wilson and Jay Freedman whose generosity in helping those on the Microsoft forums is amazing. I used their macros (with very minor adaptations) to achieve what Mary wanted.

Find the Alt Text description in the HTML file

Use this method if you only have a few images with Alt Text.

  1. Open the Word document.
  2. Save as an HTML file.
  3. Open the saved HTML file in a text editor (e.g. NotePad).
  4. Search for alt=.
  5. If the alt= information is a file name/path, copy that into another document so you have a list of them.

Macro to display the Alt Text for each image

This macro from John SR Wilson runs through your document and pops up a message box for each image it finds. The message box displays the Alt Text if there is any, otherwise it tells you that no Alt Text was found for that image. Keep clicking OK until the end (you can’t cancel). NOTE: The code runs off the screen, so copy/paste all lines—don’t try to type them out.

Sub getAltText()
'
' getAltText Macro
' Identifies if Alt Text is associated with an image or not. If so, displays the Alt Text; if not, says No Alt Text
' from: John SR Wilson, at https://answers.microsoft.com/en-us/office/forum/office_2010-customize/how-do-i-access-alt-text-for-pictures-with-vba/b3d196a6-b31f-4f28-90bb-be4f7e73958c?db=5

Dim oshp As InlineShape
For Each oshp In ActiveDocument.InlineShapes
oshp.Select
If oshp.AlternativeText <> "" Then
MsgBox "Alt Text is " & oshp.AlternativeText
Else
MsgBox "No Alt Text"
End If
Next

End Sub

Macro to extract the Alt Text from each image and put it into a separate document

This macro from Jay Freedman first asks you to select the file you want to run the macro on (it doesn’t have to be the currently open file), then a few seconds later, it asks where you want to save the new document containing all the Alt Text. NOTE: The code runs off the screen, so copy/paste all lines—don’t try to type them out.

Sub ExportAltText()
' Exports all Alt Text for inline images in a selected document into a separate file, ready for translation
' From Jay Freedman, at https://answers.microsoft.com/en-us/office/forum/office_2007-word/need-word-macro-to-export-and-import-alt-text/af9d12fc-8eab-4343-a25d-f086ae7d73b3

    Dim strPictures As String
    Dim docPictures As Document
    Dim docTranslate As Document
    Dim objInlinePic As InlineShape
    Dim objFloatPic As Shape
    Dim tblTranslate1 As Table
    Dim tblTranslate2 As Table
    Dim tblLoop As Table
    Dim rowCurrent As Row
    Dim oRg As Range

    MsgBox "In the next dialog, select the file containing " & _
        "the pictures whose alt text will be translated."
    strPictures = GetFileName()
    If strPictures = "" Then Exit Sub

    On Error GoTo BadInputFile
    Set docPictures = Documents.Open(FileName:=strPictures)

    Set docTranslate = Documents.Add
    With docTranslate
        ' set up header and footer in translation document
        .Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = _
            "Alt Text of " & docPictures.FullName
        Set oRg = .Sections(1).Footers(wdHeaderFooterPrimary).Range
        oRg.Text = vbTab
        oRg.Collapse wdCollapseEnd
        .Fields.Add Range:=oRg, Type:=wdFieldPage, PreserveFormatting:=False

        ' create two 2x2 tables
        Set tblTranslate1 = .Tables.Add(Range:=.Range, numrows:=2, numcolumns:=2)


        Set oRg = .Range
        oRg.InsertParagraphAfter
        Set oRg = .Range
        oRg.Collapse wdCollapseEnd

        Set tblTranslate2 = .Tables.Add(Range:=oRg, numrows:=2, numcolumns:=2)

        ' put the docPictures path & filename in a document variable
        ' so the import macro can locate it
        .Variables("docPictures").Value = docPictures.FullName
    End With

    ' put a heading row in the table and set borders
    For Each tblLoop In docTranslate.Tables
        With tblLoop
            .Cell(1, 1).Range.Text = "Original Alt Text"
            .Cell(1, 2).Range.Text = "Translated Alt Text"
            .Rows(1).Range.Font.Bold = True
            .Rows(1).HeadingFormat = True
            .Borders.InsideColor = wdColorAutomatic
            .Borders.InsideLineStyle = wdLineStyleSingle
            .Borders.OutsideColor = wdColorAutomatic
            .Borders.OutsideLineStyle = wdLineStyleSingle
        End With
    Next tblLoop

    ' put the alt text of each inline picture into the first column of the table's
    ' last row, and add a new empty row below it
    On Error Resume Next
    For Each objInlinePic In docPictures.InlineShapes
        If objInlinePic.AlternativeText <> "" Then
            tblTranslate1.Rows.Last.Cells(1).Range.Text = objInlinePic.AlternativeText
            If Err.Number <> 0 Then
                MsgBox "Error " & Err.Number & vbCr & Err.Description
                Err.Clear
            End If
            tblTranslate1.Rows.Add
        End If
    Next objInlinePic
    tblTranslate1.Rows.Last.Delete

    ' put the alt text of each floating picture into the first column of the table's
    ' last row, and add a new empty row below it
    On Error Resume Next
    For Each objFloatPic In docPictures.Shapes
        If objFloatPic.AlternativeText <> "" Then
            tblTranslate2.Rows.Last.Cells(1).Range.Text = objFloatPic.AlternativeText
            If Err.Number <> 0 Then
                MsgBox "Error " & Err.Number & vbCr & Err.Description
                Err.Clear
            End If
            tblTranslate2.Rows.Add
        End If
    Next objFloatPic
    tblTranslate2.Rows.Last.Delete

    docPictures.Close wdDoNotSaveChanges
    docTranslate.Save

    Exit Sub

BadInputFile:
    MsgBox "The file " & strPictures & " could not be opened." & _
        vbCr & "Error " & Err.Number & vbCr & Err.Description
End Sub

Function GetFileName() As String
    Dim dlg As FileDialog

    Set dlg = Application.FileDialog(msoFileDialogFilePicker)
    If dlg.Show <> -1 Then
        GetFileName = ""
    Else
        GetFileName = dlg.SelectedItems(1)
    End If
End Function

[Links last checked August 2018]

 

One comment

  1. […] The official blog of CyberText Consulting – technical communication specialists « Word: Extract Alt Text from images […]



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: