Inserting Pictures into Comment Box with VBA

lp3eb

New Member
Joined
Mar 22, 2013
Messages
11
Hi everyone,

So here is what I would like to do:

1 - Highlight certain cells in a worksheet. Typically a sequence of cells in a column -- top to bottom
2 - Run my macro at which time the dialog file picker pops up and I select a series of images from a folder on my hard drive. The number of images is equal to the number of cells highlighted
3 - Macro inserts comments in each cell
4 - Macro formats (sizes) each comment accordingly
5 - Macro inserts picture into each comment in sequence (eg first picture goes into first cell's comment, second picture goes into second cell's comment, etc)

So far I've gotten through item #4 without a problem. The tricky part for me is how to fill each comment with a corresponding picture.
Any ideas??

Here is my code so far:

Code:
Sub PictureInComments()
    Dim n As Integer
    Dim i As Integer
    Dim cmt As Comment
    Dim c As Range
    Dim rng As Range
    Dim item
    
    On Error Resume Next
    
    i = 1
    
    Set rng = Application.Selection
   
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .Title = "Select Images"
        .ButtonName = "Select"
        .Show
        For Each c In rng
            n = i
            c.AddComment
            Set cmt = c.Comment
            If Not cmt Is Nothing Then
                With cmt.Shape
                    .Height = 162
                    .Width = 296
                    For Each item In .SelectedItems
                        .Fill '***HERE I WANT TO PLACE THE FIRST PICTURE IN THE FIRST CELL, THEN SECOND PICTURE IN THE SECOND CELL, ETC BUT HOW DO I DO THAT?***
                    Next item
                End With
            End If
        Next c
        i = i + 1
    End With


End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Well I suppose I was able to work it out on my own. It works great. Here is my final code:

Code:
Sub PicsInComments()
    Dim n As Integer
    Dim i As Integer
    Dim cmt As Comment
    Dim c As Range
    Dim rng As Range
    Dim strPic As String
    
    On Error Resume Next
    Set rng = Application.Selection
    i = 1
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .Title = "Select Images"
        .ButtonName = "Select"
        If .Show <> -1 Then
            Exit Sub
        End If
        
        n = .SelectedItems.Count
        
        For Each c In rng
            c.AddComment
            Set cmt = c.Comment
            If Not cmt Is Nothing Then
            strPic = .SelectedItems(i)
                With cmt.Shape
                    .Height = 162
                    .Width = 296
                    .Fill.UserPicture strPic
                End With
            End If
            i = i + 1
            If i = n + 1 Then
                Exit Sub
            End If
       Next c
       
    End With
End Sub
 
Upvote 0
HI ipe3b,
Can you please help
can i insert the images for file path, not web URL, but local folder file path in selected range to selected destination range ?
Thanks
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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