How do I convert URL (in a worksheet) to an image - excel

I have this code to convert a set of URLs in column B to images in column C, but i get the error :
Unable to get the Insert property of the Pictures class. My code :
Private Sub Insert_Pic()
Dim pic As String
Dim myPicture As Picture
Dim rng As Range
Dim item As Range
lRow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("B3:B" & lRow)
For Each item In rng
pic = item.Offset(0, -1)
If pic = "" Then Exit Sub
Set myPicture = ActiveSheet.Pictures.Insert(pic)
With myPicture
.ShapeRange.LockAspectRatio = msoFalse
.Width = item.Width
.Height = item.Height
.Top = Rows(item.Row).Top
.Left = Columns(item.Column).Left
.Placement = xlMoveAndSize
End With
Next
End Sub
Thanks for your help

The algorithm in debugging is to start with something tiny, that works and then to continue.
For a beginning - take this 4 lines only and run them:
Sub TestMe()
Dim myPicAddress As String
myPicAddress = "https://www.vitoshacademy.com/wp-content/uploads/2016/02/va2.png"
Dim myPic As Picture
Set myPic = ActiveSheet.Pictures.Insert(myPicAddress)
End Sub
Then, start working on your code, putting the With-End With part to the code, that already works:
Sub TestMe2()
Dim myPicAddress As String
myPicAddress = "https://www.vitoshacademy.com/wp-content/uploads/2016/02/va2.png"
Dim myPicture As Picture
Set myPicture = ActiveSheet.Pictures.Insert(myPicAddress)
Dim item As Range
Set item = ActiveSheet.Cells(5, 5)
With myPicture
.ShapeRange.LockAspectRatio = msoFalse
.Width = item.Width
.Height = item.Height
.Top = Rows(item.Row).Top
.Left = Columns(item.Column).Left
.Placement = xlMoveAndSize
End With
End Sub
At the end, take a look at the loop and what is passed by as a picture string. Probably the error is hidden somewhere there.

Related

How to insert a picture from an existing table with file path and desired placement

I'm trying to create the following template:
The user creates a table in a "Data Entry" worksheet that lists the following:
File path ie: P:\Phone Camera Dump\20121224_111617.jpg
Range where the picture is to be placed in the "PICS" worksheet.
Once the list is finalized, the user executes and images are placed within the ranges specified on the "PICS" worksheet and dynamically re-sized.
Presently the range has a set width of 624px and a height of 374px, but ideally, I would like the image to resize (aspect ratio not locked) dynamically in the width and height change.
I've used the following code as a base but am struggling with how to incorporate the cell ranges instead of the static row updates:
Sub InsertSeveralImages()
Dim pic_Path As String 'File path of the picture
Dim cl As Range, Rng As Range
Dim WS_Templte As Worksheet
Set WS_Templte = Worksheets("PICS")
Set Rng = Worksheets("Data Entry").Range("C13:C42")
pastingRow = 2
For Each cl In Rng
pic_Path = cl.Value
Set InsertingPicture = WS_Templte.Pictures.Insert(pic_Path)
'Setting of the picture
With InsertingPicture
.ShapeRange.LockAspectRatio = msoTrue
.Height = 100
.Top = WS_Templte.Rows(pastingRow).Top
.Left = WS_Templte.Columns(3).Left
End With
pastingRow = pastingRow + 5
Next cl
Set myPicture = Nothing
WS_Templte.Activate
End Sub
Any thoughts?
I figured it out. Here is the code in case anyone wants to use it:
Public Sub InsertPictures()
Dim vntFilePath As Variant
Dim rngFilePath As Range
Dim vntPastePath As Variant
Dim rngPastePath As Range
Dim lngCounter As Long
Dim pic As Picture
Set WS_Templte = Worksheets("PICS")
On Error GoTo ErrHandler
With ThisWorkbook.Sheets("PICS") '<-- Change sheet name accordingly
' Set first cell containing a row number
Set rngFilePath = .Range("BJ7")
vntFilePath = rngFilePath.Value
' Set first cell containing a paste range
Set rngPastePath = .Range("BK7")
vntPastePath = rngPastePath.Value
Do Until IsEmpty(vntFilePath)
If Dir(vntFilePath) = "" Then vntFilePath = strNOT_FOUND_PATH
Set pic = .Pictures.Insert(vntFilePath)
lngCounter = lngCounter + 1
With pic
.ShapeRange.LockAspectRatio = msoFalse
If .ShapeRange.Rotation = 90! Or .ShapeRange.Rotation = 270! Then
.Height = Application.CentimetersToPoints(16.3)
.Width = Application.CentimetersToPoints(10.03)
.Top = WS_Templte.Rows(rngPastePath).Top - (.Height - .Width) / 2#
.Left = WS_Templte.Columns(4).Left + (.Height - .Width) / 2#
Else
.Width = Application.CentimetersToPoints(10.03)
.Height = Application.CentimetersToPoints(16.3)
.Top = WS_Templte.Rows(rngPastePath).Top
.Left = WS_Templte.Columns(4).Left
End If
End With
Set rngFilePath = rngFilePath.Offset(1)
vntFilePath = rngFilePath.Value
Set rngPastePath = rngPastePath.Offset(1)
vntPastePath = rngPastePath.Value
Loop
End With
MsgBox lngCounter & " pictures were inserted.", vbInformation
ExitProc:
Set rngFilePath = Nothing
Set pic = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitProc
End Sub

How to position image in a cell range using Excel VBA?

I am trying to move an image within a certain cell range.
I download it and add it up to my specified Excel sheet.
The image looks like this: https://imgur.com/GteP0pM
I would like to resize the image to fit within a range like:
Set r = ws.Range("C17:O34")
To look something like this: https://imgur.com/rddltWk
The image can be a resized manually if need, but I need it within that cell range.
To select the image I tried:
Sub selectImage12()
Worksheets("T-tilbud").Shapes.Range(Array("Picture 12")).Select
End Sub
To move it to the specified cells, I tried:
Set r = ws.Range by following this example:
Dim r As Range
Dim ws As Worksheet
Dim imagePath As String
Dim img As Picture
Set ws = Worksheets("CheckListIndustrialisation")
Set r = ws.Range("A1:D4")
imagePath = "C:\myImage.jpg"
Set img = ws.Pictures.Insert(imagePath)
With img
.ShapeRange.LockAspectRatio = msoFalse
.Top = r.Top
.Left = r.Left
.Width = r.Width
.Height = r.Height
End With
if you want to apply code that you've specified in example to selected "Picture 12" then you can use this new example:
Sub selectImage12()
Dim r As Range
Dim ws As Worksheet
Dim img As ShapeRange
Set ws = Worksheets("T-tilbud")
Set r = ws.Range("A1:D4")
Set img = ws.Shapes.Range(Array("Picture 12"))
With img
.LockAspectRatio = msoFalse
.Top = r.Top
.Left = r.Left
.Width = r.Width
.Height = r.Height
End With
End Sub

Load image to fit in merged cell

I have a table that contains the file path, when the button is clicked the macro will display an image according to the url path. Here is my code (sourch : Link)
Sub Macro_1()
Dim cShape As Shape
Dim cRange As Range
Dim cColumn As Long
On Error Resume Next
Application.ScreenUpdating = False
Set xRange = ActiveSheet.Range("C5:D6, G5:H6, C8:D9, G8:H9")
For Each cell In xRange
cName = cell
ActiveSheet.Pictures.insert(cName).Select
Set cShape = Selection.ShapeRange.Item(1)
If cShape Is Nothing Then GoTo line22
cColumn = cell.Column
Set cRange = Cells(cell.Row, cColumn)
With cShape
.LockAspectRatio = msoFalse
.Height = cRange.Height - 5
.Width = cRange.Width - 5
.Top = cRange.Top + 2
.Left = cRange.Left + 2
.Placement = xlMoveAndSize
End With
line22:
Set cShape = Nothing
Next
Application.ScreenUpdating = True
End Sub
The code works as shown in the following illustration.
But I want the image to be in all merged cells. As shown in the following picture
Please let me know if you see anything that will fix this! I'm sure it's something simple, but I've been stuck for a while on this one.
You can use the MergeArea property of the Range object to return the merged range. Your macro can amended as follows (untested) . . .
Sub Macro_1()
Dim cShape As Shape
Dim cRange As Range
Dim cColumn As Long
On Error Resume Next
Application.ScreenUpdating = False
Set xRange = ActiveSheet.Range("C5, G5, C8, G8")
For Each cell In xRange
cName = cell
ActiveSheet.Pictures.Insert(cName).Select
Set cShape = Selection.ShapeRange.Item(1)
If cShape Is Nothing Then GoTo line22
cColumn = cell.Column
Set cRange = cell.MergeArea
With cShape
.LockAspectRatio = msoFalse
.Height = cRange.Height - 5
.Width = cRange.Width - 5
.Top = cRange.Top + 2
.Left = cRange.Left + 2
.Placement = xlMoveAndSize
End With
line22:
Set cShape = Nothing
Next
Application.ScreenUpdating = True
End Sub

How to add images to the cells in excel vba

I want to add an image to Cell B1,
and then put the same images to B15, B29, B43, ......B57 (which are increasing by 14) at once
I searched for the ways to do this, but couldn't find how to.
Could someone please tell me how to do this?
Option 1 based on this solution
Option Explicit
Sub TiragePictures()
Const PicPath = "c:\PPP\AAA.png" ' your own path to the image
Dim ws As Worksheet, r As Long, cell As Range
Set ws = ActiveSheet
For r = 15 To 57 Step 14
Set cell = ws.Cells(r, "B")
With ws.Pictures.Insert(PicPath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 70
.Height = 50
End With
.Left = cell.Left
.Top = cell.Top
.Placement = 1
.PrintObject = True
End With
Next
End Sub
Option2 with Shapes.AddPicture method
Sub TiragePictures2()
Const PicPath = "c:\PPP\AAA.png" ' your own path to the image
Dim ws As Worksheet, r As Long, cell As Range, sh As Shape
Set ws = ActiveSheet
For r = 15 To 57 Step 14
Set cell = ws.Cells(r, "B")
With ws.Shapes.AddPicture(Filename:=PicPath, LinkToFile:=False, _
SaveWithDocument:=True, Left:=cell.Left, _
Top:=cell.Top, Width:=-1, Height:=-1) '-1 retains the width/height of the existing file
.LockAspectRatio = True 'before resizing, set the proportions to keep
.Width = 70
.Height = 50
End With
Next
End Sub

Userform works despite Run-Time Error 91. What's going on?

I am new to Userforms. I have created the following Userform which is called from a subroutine. The Userform picks up a range from a sheet and creates a corresponding number of textboxes and then checkboxes so as to allocate an original name with a new name.
The userform is created with the following:
Public Sub UserForm_Initialize()
'Declare variables
Dim txtBox As MSForms.TextBox
Dim comBox As MSForms.ComboBox
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim dist As Integer
Dim dstArr As Variant
Dim rng As Range
'Assign variables
Set rng = Range("Missing_MAERSK")
n = rng.Rows.Count
dist = 5
dstArr = Range("LU_Destination_Ports").Value
'Loop to add textboxes
For i = 1 To n
Set txtBox = UserForm1.Controls.Add("Forms.TextBox.1", Visible:=True)
With txtBox
.name = "txtBox" & i
.Value = rng(i)
.Height = 20
.Width = 150
.Left = 81
.Top = 30 + dist
.Font.Size = 10
End With
dist = dist + 20
Next i
'Loop to add list boxes
dist = 5
For j = 1 To n
Set comBox = UserForm1.Controls.Add("Forms.ComboBox.1", Visible:=True)
With comBox
.name = "comBox" & j
.List = dstArr
.Height = 20
.Width = 150
.Left = 315
.Top = 30 + dist
.Font.Size = 10
End With
dist = dist + 20
Next j
'Show userform
UserForm1.Show
End Sub
And then when the Replace Names button is clicked the following is ran:
Public Sub CommandButton1_Click()
'Close userform
Unload UserForm1
'This is the one
Dim cmb As MSForms.ComboBox
' Dim txt As MSForms.TextBox
Dim oldVal As String
Dim newVal As String
Dim rng As Range
Dim rng2 As Range
Dim n As Integer
Set rng = Range("MAERSK_Destin")
Set rng2 = Range("Missing_MAERSK")
n = rng2.Rows.Count
'Loop
For i = 1 To n
Set txt = Me.Controls("txtBox" & i)
Set cmb = Me.Controls("comBox" & i)
If cmb.Value <> "" Then
oldVal = txt.Value
newVal = cmb.Value
rng.Replace what:=oldVal, Replacement:=newVal
End If
Next i
End Sub
Let's say I populate Bangkok to Bangkok BMT, I get the following:
I think the issue might be with the way I call the values in the Command_Button1_Click sub.
Any advice would be appreciated.
Cheers
Figured out the problem.
According to this post: Call UserForm_Initialize from Module
a UserForm should not be initialized from outside the userform.
I was calling UserForm_Initialize() from my sub, so to rectify this I replaced it with UserForm1.Show

Resources