Need Help For Change VBA Code

Bysystemroad

New Member
Joined
Jun 1, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Dear Users

You can see the VBA code that ı use in the below.

But ı need to transform to "ActiveSheet.Shapes.AddPicture" style.

I use in the below code for to take the photos in the relevant folder, which corresponds to the product codes in column B, put pictures to column A with.

But when ı send to my customer, they can't see pictures in the excel .

VBA Code:
Sub InsertPictures()
  Dim objPic As Picture, i As Long
  Dim sPath As String, sFile As String
  
  sPath = "C:\Users\canberk.saka\Desktop\SS20 FOTOĞRAFLARI\SS 2020 IMAGES\SS20 FOTOS\"

  If Dir(sPath, vbDirectory) = "" Then
    MsgBox "This directory does not exist"
    Exit Sub
  End If
  
  On Error Resume Next
  For Each objPic In ActiveSheet.Pictures
    If objPic.Name Like "img_*" Then
      objPic.Delete
    End If
  Next
  On Error GoTo 0
  
  For i = 4 To Range("B" & Rows.Count).End(3).Row
    sFile = Range("B" & i).Value & ".jpg"
    If Dir(sPath & sFile) <> "" Then
      Set objPic = ActiveSheet.Pictures.Insert(sPath & sFile)
      With Range("A" & i)
        objPic.ShapeRange.LockAspectRatio = msoFalse
        objPic.Top = .Top
        objPic.Left = .Left
        objPic.Width = .Width
        objPic.Height = .Height
        objPic.Name = "img_" & i
      End With
    End If
  Next
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this

Rich (BB code):
Sub InsertPictures()
  Dim objPic As Shape, i As Long
  Dim sPath As String, sFile As String
  
  sPath = "C:\Users\canberk.saka\Desktop\SS20 FOTOGRAFLARI\SS 2020 IMAGES\SS20 FOTOS\"

  If Dir(sPath, vbDirectory) = "" Then
    MsgBox "This directory does not exist"
    Exit Sub
  End If
  
  On Error Resume Next
    For Each objPic In ActiveSheet.Pictures
      If objPic.Name Like "img_*" Then
        objPic.Delete
      End If
    Next
  On Error GoTo 0
  
  For i = 4 To Range("B" & Rows.Count).End(3).Row
    sFile = Range("B" & i).Value & ".jpg"
    If Dir(sPath & sFile) <> "" Then
      With Range("A" & i)
        objPic = ActiveSheet.Shapes.AddPicture(Filename:=sPath & sFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
        objPic.Name = "img_" & i
      End With
    End If
  Next
End Sub
 
Upvote 0
Thanks for quick answer.

But actions is like this.

1591265388837.png
 
Upvote 0
oops - I forgot to Set the object :oops:
amend this line
Rich (BB code):
Set objPic = ActiveSheet.Shapes.AddPicture(Filename:=sPath & sFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
 
Upvote 0
Is the messagebox giving the correct path to your image?

Rich (BB code):
INSERT THIS LINE
MsgBox sPath & sFile
AVOVE THIS LINE
        Set objPic = ActiveSheet.Shapes.AddPicture(Filename:=sPath & sFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
 
Upvote 0
Does that mean you have solved the problem?
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

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