Forum Discussion
lazuli1485
Nov 09, 2022Copper Contributor
VBA code fails in newest build
Hi, I have had this line of code in a macro for many years Set ImageToAdd = ActiveSheet.Shapes.AddPicture(ImageURL, True, False, _
Cells(RowToAddImage, ColumnToAddImage).Left + 6, _
Ce...
lazuli1485
Nov 12, 2022Copper Contributor
Hi,
Both are valid. I've stripped the code back and it can be inserted into a blank workbook to test:
Private Sub CommandButton1_Click()
Dim ImageToAdd
Dim RowToAddImage
RowToAddImage = 2
Dim ColumnToAddImage
ColumnToAddImage = 7
Dim ImageURL As String
'ImageURL = "https://peters.co.uk/rgw/assets/covers/large/513/9781910002513.jpg" ' fails
ImageURL = "https://peters.co.uk/uploads/downloads/medium/9781910002513.jpg" 'same image, same website, works
Set ImageToAdd = ActiveSheet.Shapes.AddPicture(ImageURL, True, False, _
Cells(RowToAddImage, ColumnToAddImage).Left + 6, _
Cells(RowToAddImage, ColumnToAddImage).Top + (Cells(RowToAddImage, ColumnToAddImage).Height / 2), -1, -1)
End Sub
As you can see I've narrowed the error down - it is conditional upon the image source. It will work with an image hosted on Google or Amazon. It works with the image stored in one valid web location, but will not work in another.
My assumption at this point is that the newer builds is not treating the fail location as secure... but I have no idea why this might be the case.
mtarler
Nov 12, 2022Silver Contributor
Not much more help but a few things I checked:
the files are the exact same but if i download both either will work from local drive
I got same results using ActiveSheet.Pictures.Insert(ImageURL)
i wonder if there is any issue with server location in that the one that doesn't work appears to be inside someone's account (https://peters.co.uk/rgw/assets/...) as opposed to the other that appears to be a public location (https://peters.co.uk/uploads/...). I know both can be viewed in a browser without any log in but maybe the site permissions for those location are different. I wonder if this has anything to do with the cross-origin sharing standard (CORS).
the files are the exact same but if i download both either will work from local drive
I got same results using ActiveSheet.Pictures.Insert(ImageURL)
i wonder if there is any issue with server location in that the one that doesn't work appears to be inside someone's account (https://peters.co.uk/rgw/assets/...) as opposed to the other that appears to be a public location (https://peters.co.uk/uploads/...). I know both can be viewed in a browser without any log in but maybe the site permissions for those location are different. I wonder if this has anything to do with the cross-origin sharing standard (CORS).
- lazuli1485Nov 14, 2022Copper ContributorThank you mtarler.
I've found a workaround for this by referencing the images in a different way.
The URLS are valid and were previously considered valid by Excel - so something has changed, and this may trip up others in the future.- mtarlerNov 14, 2022Silver Contributori suspect the change was something like a small update to an underlying library that now includes that CORS thing of the like. That said, can you share what your solution is, how you are referencing the images now so others may know how to fix the problem if they have it happen.