Problem with OnError when insterting pictures

cocopops2001

Board Regular
Joined
Apr 18, 2011
Messages
112
I have a problem with an OnError statement within my code.

What I would like is for my code to run a loop and extract a filename using the information in columns 1 and 2 of specific range and then insert this picture into the last column of same range. This bit works fine.

If the file cannot be found then I would like the last column to say "No Photo Found". This partially works as the No Photo Found appears in row 2 of my range but from then on I get the error 'unable to get the Insert property of the Picture class'. I know this is because the file is not available but in this case it should be putting "No Photo Found" into the cell instead of the picture.

Code below. Any help would be great.

Code:
Sub RefreshList()
    Application.ScreenUpdating = False
    
    Range("B4").Select
    Selection.End(xlDown).Select
    EndRow = Selection.Row
    
    Range("B4:H" & EndRow).Select
      
    With Selection
        ActiveWorkbook.Names.Add Name:="StaffList", RefersTo:=Range("B4:H" & EndRow)
    End With
    
    With Range("Stafflist")
    .Rows(RowNum).RowHeight = 130#
    End With
       
    Range("H4").Select
    
    Range("H4:H" & EndRow).Select
      
    With Selection
        ActiveWorkbook.Names.Add Name:="PhotoRange", RefersTo:=Range("H4:H" & EndRow)
    End With
          
RowNum = 1
    
PicInsert:
Do While Range("stafflist").Cells(RowNum, 1) <> 0
    
'On Error GoTo ErrNoPhoto:
    Dim picname As String
     
    picname = Range("Stafflist").Cells(RowNum, 1) & " " & Range("Stafflist").Cells(RowNum, 2) 'This is the picture name
On Error GoTo ErrNoPhoto:
    ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\" & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & "_Photos\" & picname & ".jpg").Select 'Path to where pictures are stored
    
    With Selection
    .ShapeRange.LockAspectRatio = msoTrue
    .ShapeRange.Height = 120#
    .ShapeRange.Rotation = 0#
    .Top = Range("StaffList").Cells(RowNum, 7).Top
    .Left = Range("StaffList").Cells(RowNum, 7).Left
    .Placement = xlMoveAndSize
    .Name = picname
    End With
        
    RowNum = RowNum + 1
    
Loop
    
    Application.ScreenUpdating = True
    Exit Sub
ErrNoPhoto:
            
    Range("StaffList").Cells(RowNum, 7) = "No Photo Found"
    
    RowNum = RowNum + 1
        
    GoTo PicInsert
    
    Exit Sub
Application.ScreenUpdating = True
End Sub
 
Code:
Set oPic = ActiveSheet.Shapes.AddPicture(sPicPath & picname & ".jpg")

Problem with the above line. Error 450 Wrong number of arguments or invalid property assignment.

Any ideas?
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Set oPic = ActiveSheet.Shapes.AddPicture(sPicPath & picname & ".jpg")

Problem with the above line. Error 450 Wrong number of arguments or invalid property assignment.

Any ideas?

Solved by using

Code:
Set oPic = ActiveSheet.Pictures.Insert(sPicPath & picname & ".jpg")
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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