How can I use VBA coding to make word automatically remember the filename of a dragged and dropped image?

38 Views Asked by At

I have tried to drag and drop an image into MS Word that should contain the filename without displaying it on screen. Then when a macro is run using Alt+F8 it should retrieve the filename. Can you please help me by viewing below or coming up with alternative solutions? It could really help me a lot when a teacher wants a file represented in a report afterwards.

I have tried to do it using the following macros inserted as modules:

Option Explicit

' Enable dropped images
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    ActiveDocument.InlineShapes.AddPicture FileName:="", LinkToFile:=False, SaveWithDocument:=True
End Sub

' Capture the filename when an image is dropped
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Title = "MyImageControl" Then
        ActiveDocument.Bookmarks("ImageFilename").Range.Text = ContentControl.Range.InlineShapes(1).LinkFormat.SourceFullName
    End If
End Sub

' Retrieve the image filename from the bookmark
Sub RetrieveImageName()
    Dim imageName As String
    imageName = ActiveDocument.Bookmarks("ImageFilename").Range.Text
    MsgBox "Image Name: " & imageName, vbInformation, "Image Information"
End Sub
0

There are 0 best solutions below