Code does not Extract the Images from some URL - excel

I have been using this code which works for some URL but not for all I really do not why. Then I have tried with different available codes online but no success.
Your help will be really appreciated in this regards.
Option Explicit
Dim rng As Range
Dim cell As Range
Dim Filename As String
Sub URLPictureInsert()
Dim theShape As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
Set rng = ActiveSheet.Range("A1:A3000") ' <---- ADJUST THIS
For Each cell In rng
Filename = cell
If InStr(UCase(Filename), "JPG") > 0 Then ' <--- USES JPG ONLY
ActiveSheet.Pictures.Insert(Filename).Select
Set theShape = Selection.ShapeRange.Item(1)
If theShape Is Nothing Then GoTo isnill
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
With theShape
.LockAspectRatio = msoFalse
.Width = 20
.Height = 20
.Top = xRg.Top + (xRg.Height - .Height) / 2
.Left = xRg.Left + (xRg.Width - .Width) / 2
End With
isnill:
Set theShape = Nothing
Range("A2").Select
End If
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub
URL's
https://s3-eu-west-1.amazonaws.com/images.linnlive.com/a93f20bbb3640a1a7dc9b9a05bee8540/fc310885-cd82-49cb-bc7a-aabd08531517.jpg
https://s3-eu-west-1.amazonaws.com/images.linnlive.com/a93f20bbb3640a1a7dc9b9a05bee8540/c6c7a645-8273-40ee-87e5-1dd385111a28.jpg
https://s3-eu-west-1.amazonaws.com/images.linnlive.com/a93f20bbb3640a1a7dc9b9a05bee8540/cf9f971b-6af6-4894-a2d5-c58681adb466.jpg

Try this code below, it will Debug.Print the URL that fails to insert. Adapt to your need (if any)
Sub URLPictureInsert()
Dim rng As Range
Dim cell As Range
Application.ScreenUpdating = False
With ActiveSheet
Set rng = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row) ' <---- ADJUST THIS
End With
For Each cell In rng
If InStr(UCase(cell), "JPG") > 0 Then '<--- ONLY USES JPG'S
With cell.Offset(0, 1)
On Error Resume Next
ActiveSheet.Shapes.AddPicture cell, msoFalse, msoTrue, .Left + (.Width - 10) / 2, .Top + (.Height - 10) / 2, 20, 20
If Err.Number = 1004 Then Debug.Print "File not found: " & cell
On Error GoTo 0
End With
End If
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub

You will need to use On Error Resume Next, but only for the single statement that inserts the picture. And you should get rid of the Select. The Pictures.Insert-method returns the reference to the inserted image, assign this to a variable and work with that.
Additionally, I would suggest to split your code and create a routine that insert one image into a cell. Call this routine from the loop. I have implemented it as a function that returns True if it was successfull, it's up to you to decide if you want to do something if it returns False.
Function TryInsertImg(filename As String, cell As Range) As Boolean
Dim p As Picture
On Error Resume Next
Set p = cell.Parent.Pictures.Insert(filename)
If Err.Number > 0 Then Debug.Print "Couldn't insert image " & Err.Number & "-" & Err.Description
On Error GoTo 0
If p Is Nothing Then
Exit Function
End If
Dim theShape As Shape
Set theShape = p.ShapeRange.Item(1)
With theShape
.LockAspectRatio = msoFalse
.Width = 20
.Height = 20
.Top = cell.Top + (cell.Height - .Height) / 2
.Left = cell.Left + (cell.Width - .Width) / 2
End With
TryInsertImg = True
End Function
Your calling routine could look like this:
For Each cell In rng
Filename = cell
If InStr(UCase(Filename), "JPG") > 0 Then '<--- ONLY USES JPG'S
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
If Not TryInsertImg(filename, xRg) then
xRg = "(error loading image)"
End If
End If
Next cell

Related

How to save inserted pictures in excel using VBA?

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.
Sub add_pictures_R2()
Dim i%, ppath$
For i = 2 To 145
' file name at column A
ppath = "C:\Users\myname\output\" & CStr(Cells(i, 1).Value) & ".png"
If Len(Dir(ppath)) Then
With ActiveSheet.Pictures.Insert(ppath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 300
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
You can do either, edit the path of the file to go along with your excel file or you could embed it. For embedding I would look at this.
https://danny.fyi/embedding-and-accessing-a-file-in-excel-with-vba-and-ole-objects-4d4e7863cfff
Its a bit messy but you would achieve what you want to do with at least the file being in the document and not trying to transfer everything with it.
Try this (using Shapes.AddPicture)
Sub add_pictures_R2()
'Note - type identifiers such as `S`, `%` are very outdated...
Dim i As Long, ppath As String, ws As Worksheet, c As Range
Set ws = ActiveSheet 'use a specific/explicit sheet reference
For i = 2 To 145
ppath = "C:\Users\myname\output\" & CStr(ws.Cells(i, 1).Value) & ".png"
Set c = ws.Cells(i, 10) 'insertion point
'passing -1 to Width/Height preserves original size
With ws.Shapes.AddPicture(Filename:=ppath, linktofile:=msoFalse, _
savewithdocument:=msoTrue, _
Left:=c.Left, Top:=c.Top, Width:=-1, Height:=-1)
.LockAspectRatio = msoTrue
.Placement = xlMove
.Height = .Height / 2 'size to 50%
End With
Next i
End Sub
I got the answer from Jimmypop at mrexcel. It worked.
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

Save picture as a picture instead of link when using VBA in Excel

I have an Excel file for label template with 6,300 items (each item has a parent ID which matches the picture name that suits the child item).
I found code that will run all the way through without an error (when items are missing for example).
However when share the item it has the pictures saved as a link instead of a picture, and whoever receive that file will have a broken link message.
Sub Picture()
Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long
lastrow = Worksheets("sheet2").Range("b1").CurrentRegion.Rows.Count
x = 2
For x = 2 To lastrow
On Error GoTo errhandler:
Set pastehere = Cells(x, 1)
pasterow = pastehere.Row
Cells(pasterow, 1).Select 'This is where picture will be inserted
pictname = Cells(x, 3) 'This is the picture name
ActiveSheet.Pictures.Insert("C:\Users\BennyCohen\Pictures\Catalogue pics\" & pictname & ".jpg").Select 'Path to where pictures are stored
With Selection
.Left = Cells(pasterow, 1).Left
.Top = Cells(pasterow, 1).Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 140
.ShapeRange.Width = 80
.ShapeRange.Rotation = 0#
.linktofile = msoFalse
.savewithdocument = msoCTrue
End With
Next
errhandler:
Range("A" & x).Value = "Review"
Resume Next
End Sub
linktofile and savewithdocument are not picture properties and the error is masked by the Resume Next in the errhandler, see here. Use Shapes.addPicture().
Sub Picture()
Const FOLDER = "C:\Users\BennyCohen\Pictures\Catalogue pics\"
Dim wb As Workbook, ws As Worksheet
Dim lastrow As Long, r As Long, pictname As String
Dim n As Long, m As Long
Set wb = ActiveWorkbook ' or ThisWorkbook
Set ws = wb.Sheets("Sheet2")
lastrow = ws.Range("B1").CurrentRegion.Rows.Count
For r = 2 To lastrow
pictname = FOLDER & ws.Cells(r, 3) & ".jpg" 'This is the picture name
' check file exists
If Len(Dir(pictname)) > 0 Then
With ws.Shapes.AddPicture(pictname, _
linktofile:=msoFalse, savewithdocument:=msoTrue, _
Left:=ws.Cells(r, 1).Left, _
Top:=ws.Cells(r, 1).Top, _
Height:=140, Width:=80)
.LockAspectRatio = msoFalse
.Rotation = 0#
End With
n = n + 1
Else
ws.Cells(r, "A") = "Review"
m = m + 1
End If
Next
MsgBox n & " Pictures inserted " & _
m & " Pictures to review", vbInformation
End Sub

Show pic in column next to non empty cell in column

I'd like to show a pic loaded from network location next to each non empty cell in range A1:A10 and name the picture after the value in that range.
Sub testpics()
Dim Cell As Range
For Each Cell In Range("A1:A10").Cells
If Not IsEmpty(Cell) Then
On Error GoTo ErrNoPhoto
pictureloc = "location here" & Cell & ".jpg"
With Cell.Offset(0, 1)
Set mypict = ActiveSheet.Pictures.Insert(pictureloc)
mypict.Height = .RowHeight
mypict.Left = .Left
mypict.Top = .Top
mypict.Placement = xlMoveAndSize
mypict.Name = Cell
mypict.OnAction = "enlarge"
End With
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo for " & Cell
End If
Next Cell
End Sub
So far I came up with the code above, but it only shows the pic behind A1 if I run it.
What am I doing wrong/missing in my code?
The main issue is that you exit the loop after the first iteration. However, I refactored your code below to make it more stable, readable and better actual error trapping.
Option Explicit
Sub testpics()
Dim Cell As Range
For Each Cell In Range("A1:A10").Cells
If Not IsEmpty(Cell) Then
Dim pictureLoc As String
pictureLoc = "location here" & Cell & ".jpg"
Dim mypict As Object
On Error Resume Next
Set mypict = ActiveSheet.Pictures.Insert(pictureLoc)
On Error GoTo 0
If Not mypict Is Nothing Then
With Cell.Offset(0, 1)
mypict.Height = .RowHeight
mypict.Left = .Left
mypict.Top = .Top
mypict.Placement = xlMoveAndSize
mypict.Name = Cell.Value
mypict.OnAction = "enlarge"
End With
Else
Cell.Offset(0, 1).Value = "Could Not Find Pic"
End If
End If
Next
End Sub

Add Image Comment Excel 16 on Mac osx

I need your help! I've read many many forum and tread, but unfortunately I don't find a solution.
I'll need to build a macro that working on excel for Mac that pick an image from url and insert in a comment.
Thanks in advance
Here is a macro that working well on windows, but not in Mac osx.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngToCheck As Range
Dim n As Integer
Dim pic_file As String
Dim pict1 As Picture
n = Cells(Rows.Count, 1).End(xlUp).Row
If n = 0 Then Exit Sub
Set rngToCheck = Range(Cells(1, 2), Cells(n + 1, 2))
On Error Resume Next
If Intersect(ActiveCell, rngToCheck) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
ActiveCell.Comment.Delete
' pic_file = "http://xxx.xxx.com" & CStr(Cells(ActiveCell.Row, 1).Value) & ".jpg"
pic_file = "http://xxx.xxx.com" & CStr(Cells(ActiveCell.Row, 1).Value) & ".jpg"
Set pict1 = ActiveSheet.Pictures.Insert(pic_file)
If Not pict1 Is Nothing Then On Error Resume Next
If ActiveCell.Comment Is Nothing Then ActiveCell.AddComment
With ActiveCell.Comment.Shape
.Fill.Visible = msoTrue
.Fill.UserPicture (pic_file)
If (pict1.Width < pict1.Height) Then
.Height = 200
.Width = pict1.Width / pict1.Height * 200
Else
.Width = 200
.Height = pict1.Height / pict1.Width * 200
End If
End With
ActiveCell.Comment.Visible = False
'ActiveSheet.Shapes.SelectAll
pict1.Delete
'Selection.Delete
Application.CutCopyMode = False
Application.EnableEvents = True
' End If
End If
End Sub
In widows all works fine, in Mac I've only the empty yellow comment boxes.

Excel incorrectly placing images

I'm trying to help out a coworker with her VBA in Excel 2013. It looks like the macro is successfully pulling in the images from the designated path, but it dumps every single photo into cell A1.
Any thoughts?
Sub DeleteAllPictures()
Dim S As Shape
For Each S In ActiveSheet.Shapes
Select Case S.Type
Case msoLinkedPicture, msoPicture
S.Delete
End Select
Next
End Sub
Sub UpdatePictures()
Dim R As Range
Dim S As Shape
Dim Path As String, FName As String
'Setup the path
Path = "G:\In Transit\Carlos\BC Website images"
'You can read this value also from a cell, e.g.:
'Path = Worksheets("Setup").Range("B1")
'Be sure the path has a trailing backslash
If Right(Path, 1) <> "\" Then Path = Path & "\"
'Visit each used cell in column A
For Each R In Range("B1", Range("B" & Rows.Count).End(xlUp))
'Try to get the shape
Set S = GetShapeByName(R)
'Found?
If S Is Nothing Then
'Find the picture e.g. "C:\temp\F500.*"
FName = Dir(Path & R & ".*")
'Found?
If FName <> "" Then
Set S = InsertPicturePrim(Path & FName, R)
End If
End If
If Not S Is Nothing Then
'Show the error if the name did not match the cell
If S.Name <> R Then R.Interior.Color = vbRed
With R.Offset(0, 1)
'Move the picture to the cell on the right side
S.Top = .Top
S.Left = .Left
'Resize it
S.Width = .Width
'Remove the aspect ratio by default if necessary
'S.LockAspectRatio = False
If S.LockAspectRatio Then
'Make it smaller to fit the cell if necessary
If S.Height > .Height Then S.Height = .Height
Else
'Stretch the picture
S.Height = .Height
End If
End With
'Move it behind anything else
S.ZOrder msoSendToBack
Else
R.Offset(0, 1) = "No picture available"
End If
Next
End Sub
Private Function GetShapeByName(ByVal SName As String) As Shape
'Return the shape with SName, Nothing if not exists
On Error Resume Next
Set GetShapeByName = ActiveSheet.Shapes(SName)
End Function
Private Function InsertPicturePrim(ByVal FName As String, ByVal SName As String) As Shape
'Inserts the picture, return the shape, Nothing if failed
Dim P As Picture
On Error Resume Next
'Insert the picture
Set P = ActiveSheet.Pictures.Insert(FName)
'code to resize
With P
.ShapeRange.LockAspectRatio = msoFalse
.Height = ActiveCell.Height
.Width = ActiveCell.Width
.Top = ActiveCell.Top
.Left = ActiveCell.Left
.Placement = xlMoveAndSize
End With
Set P = Nothing
'code to resize
'Success?
If Not P Is Nothing Then
'Return the shape
Set InsertPicturePrim = P.ShapeRange(1)
'Rename it, so we can easily find it later
P.Name = SName
End If
End Function
The short answer is: your macro is inserting the picture at the selected cell. Change the selection before the insert line, and you should get it inserted at each row.
Here in this example, I am selecting the cell to the left of the cell you are pulling the name value from.
If FName <> "" Then
'select the cell 1 to the left of the cell containing the image name
R.Offset(0,-1).select
Set S = InsertPicturePrim(Path & FName, R)
End If

Resources