VBA auto-add image to cell from folder

menno_edeltrend

New Member
Joined
Nov 1, 2019
Messages
14
Dear mrExcellers,

In my company we work with a product database with calculations in it, every product offcourse has an unique article number. In the shared server we have a folder with pictures of every product with the article number as the title. We want to add the pictures automatically to every product with VBA, I know it is possible but my knowledge doesn't stretch far enough.

The article number stands in column D, the picture has to been placed in column B. The cel widht and hight are minimal, it would be nice to have the picture expending when you move the mouse over it.

I would like to let VBA start in the selected row and then go on from there till there is no article number in column D. That way it doesn't overwrite all the previous uploaded pictures.

I hope there is someone out there with the solution, thanks in advance!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Ready, this code works with your file examples

VBA Code:
Sub Auto_Add_Image_2()
  Dim i As Long, wPath As String, wFile As String, sh As Worksheet
  Dim h As Variant, w As Variant
  If ActiveCell.Column <> 3 Then
    MsgBox "Select column C"
    Exit Sub
  End If
  If ActiveCell.Value = "" Then
    MsgBox "Select article number in column C"
    Exit Sub
  End If
  Set sh = ActiveSheet
   With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Selecteer map"
    .AllowMultiSelect = False
    .InitialFileName = ThisWorkbook.Path & "\"
    If .Show <> -1 Then Exit Sub
    wPath = .SelectedItems(1) & "\"
  End With
 
  For i = ActiveCell.Row To Range("C" & Rows.Count).End(xlUp).Row
    If Range("C" & i).Value <> "" Then
      wFile = wPath & Range("C" & i).Value
      If Dir(wFile) <> "" Then
        With sh.Pictures.Insert(wFile)
          h = Range("B" & i).Height - 2
          w = Range("B" & i).Width - 2
          .ShapeRange.LockAspectRatio = True
          .Top = Range("B" & i).Top + 1
          .Left = Range("B" & i).Left + 1
          .ShapeRange.Height = h
          .ShapeRange.Width = w
          Do While True
            .ShapeRange.Height = h
            .ShapeRange.Width = w
            If .Width < Range("B" & i).Width And .Height < Range("B" & i).Height Then Exit Do
            h = h - 5
            w = w - 5
          Loop
          
          With Range("B" & i).AddComment
            .Shape.Fill.UserPicture wFile
            .Shape.ScaleHeight 4, msoFalse, msoScaleFromTopLeft
            .Shape.ScaleWidth 3.25, msoFalse, msoScaleFromTopLeft
          End With
        End With
      Else
        MsgBox "The file does not exist: " & wFile
      End If
    End If
  Next
End Sub
 
Upvote 0
Try this.
But I think the following is about the aspect of the comment, not about the image within the comment.
But check if it is what you need.

VBA Code:
          With Range("B" & i)
            .AddComment
            .Comment.Shape.Fill.UserPicture wFile
            .Comment.Shape.ScaleHeight 4, msoFalse, msoScaleFromTopLeft
            .Comment.Shape.ScaleWidth 3.25, msoFalse, msoScaleFromTopLeft
            .Comment.Shape.LockAspectRatio = msoTrue
          End With
 
Upvote 0
No that is indeed not working unfortunately. I think what makes it hard to do is that the picture becomes the fill of the comment and does not stay a picture.
 
Upvote 0
I was investigating and did not find properties of the image within the comment. The properties are from the comment.

In addition to inserting the image and putting the image in the comment I think your file will grow too much.
You can zoom the sheet to see the image larger, or make the cells larger so that the image is in a larger space.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top