How to save VBA inserted pictures within the excel?

Chemist Yang

New Member
Joined
Jul 27, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I was trying to insert some pictures that are saved on my desktop to an excel file.

I found that some online codes worked well. But it seemed that those inserted pictures were not saved with the documents - the inserted pictures won't be displayed when I opened the file on another computer. I am wondering how I should tweak the codes so it can save the inserted pictures within the excel? If possible with VBA, how to adjust the inserted pictures to their 50% dimensions? I am completely new to VBA. Sorry for this basic question.

VBA Code:
Sub add_pictures_R2()
Dim i%, ppath$
For i = 2 To 145
    ' file name at column A
    ppath = "C:\Users\YANG\output\" & CStr(Cells(i, 1).Value) & ".png"
    If Len(Dir(ppath)) Then
        With ActiveSheet.Pictures.Insert(ppath)
        With .ShapeRange
            .LockAspectRatio = msoTrue
            .Width = 23
            .Height = 100
        End With
        .Left = ActiveSheet.Cells(i, 10).Left
        .Top = ActiveSheet.Cells(i, 10).Top
        .Placement = 1
        .PrintObject = True
    End With
    End If

   
Next
End Sub
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi There

Will the below work for you?

VBA Code:
Sub add_pictures_R2()
    Const folderPath As String = "C:\Users\YANG\output\"
    Dim r As Long
    Application.ScreenUpdating = False
    With ActiveSheet
        For r = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
            If Dir(folderPath & .Cells(r, "A").Value & ".png") <> vbNullString Then
                .Shapes.AddPicture Filename:=folderPath & .Cells(r, "A").Value & ".png", _
                                   LinkToFile:=False, SaveWithDocument:=True, _
                                   Left:=ActiveSheet.Cells(r, 10).Left, Top:=ActiveSheet.Cells(r, 10).Top, Width:=.Cells(r, "C").Width, Height:=.Cells(r, "C").Height
            
            Else
                .Cells(r, "B").Value = "Not found"
            End If
            DoEvents
        Next
    End With
        Set myDocument = Worksheets(1)
For Each s In myDocument.Shapes
    Select Case s.Type
    Case msoLinkedPicture, msoPicture
        s.ScaleHeight 0.5, msoTrue
        s.ScaleWidth 0.5, msoTrue
    Case Else
'       Do Nothing
    End Select
Next
    Application.ScreenUpdating = True
        MsgBox "Done"
End Sub
 
Upvote 0
Solution
Thanks for the feedback and glad we could help...
 
Upvote 0
Thanks so much! The code worked!
Maybe just an explanation why your images disappeared opening the file on another pc...

The way your code worked is it did insert the picture but was not taking into account that it should remain there... It inserted it a link... So whenever you opened on another pc the file was looking for the filepath you specified as well as the file...

Obviously this file and filepath does not exist on the other pc...hence it did not show ..
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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