Home > front end >  Macro to extract images from a document and add their filepaths as a caption
Macro to extract images from a document and add their filepaths as a caption

Time:01-14

I have 200 Word documents that include multiple images. I'm looking for a way to automatically put the filepaths of the images below them. I know how to run macros but I am not very familiar with writing VBA code from scratch.

Here's what I'm currently doing (but it's very tedious for me since I have a lot of school documents to process):

  1. Save the DOCX file as an HTML file to extract the images
  2. Locate the folder of the saved images and copy their filepaths individually
  3. Paste the filepath below each image in the DOCX file in the format

I'm doing this because the flashcard program (Anki) that I use for med school can only import images as filepaths.

UPDATE: Here's a solution I've found so far. This is a code written by Doug Robbins that I found in the MS Word forums. I think it's similar to what I need but I want to be able to select all images instead of one at a time. I'm not sure if it's allowed to post other people's code here, please inform me if this violates the site's rules. Thank you!

Dim strImage As String

Dim ils As InlineShape

Dim FD As FileDialog

Set FD = Application.FileDialog(msoFileDialogFilePicker)

With FD

    .Title = "Select the image that you want to insert."

    .InitialView = msoFileDialogViewLargeIcons

    .Filters.Clear

    .Filters.Add "Images", "*.jpg"

    .AllowMultiSelect = False

    If .Show = -1 Then

        strImage = .SelectedItems(1)

    Else

        MsgBox "You did not select an image."

        Exit Sub

    End If

End With

Set ils = Selection.Range.InlineShapes.AddPicture(strImage, True)

ils.Select

Selection.Collapse wdCollapseEnd

Selection.InsertAfter vbCr & strImage

Selection.Collapse wdCollapseEnd

CodePudding user response:

Make your life easier and link your images when placing them in Word, instead of embedding them. In the Insert Picture dialog, the Insert button is a dropdown. Click on the arrow and choose Link to File or Link and Insert. Then the code to retrieve the path is as simple as:

Sub GetFullPath()
    On Error Resume Next
    Dim PicPath As String

    PicPath = Selection.ShapeRange(1).LinkFormat.SourceFullName
    PicPath = Selection.InlineShapes(1).LinkFormat.SourceFullName
    MsgBox PicPath
End Sub

There are two statements in there, you only need 1, depending on whether your pictures float or are inline with text.

CodePudding user response:

I played around with your code and achieved what I wanted. It turns out saving the document as Web Page, Filtered links all the images to the file. Thank you!

Sub GetFullPath()
    On Error Resume Next
    Dim PicPath As String

    PicPath = Selection.ShapeRange(1).LinkFormat.SourceFullName
    PicPath = Selection.InlineShapes(1).LinkFormat.SourceFullName

    Selection.Collapse wdCollapseEnd

    Selection.InsertAfter vbCr & "<img src=" & Chr(34) & PicPath & Chr(34) & ">"

    Selection.Collapse wdCollapseEnd
End Sub

  •  Tags:  
  • Related