How to insert pictures into Excel automatically ?
Hi - I'm trying to find a way to automatically insert and resize a picture (all saved in a specific drive)in a specific cell on a spreadsheet. s/sheet is layed out with various bits of data in rows 2 to 12 and an empty cell in row 1 (repeated in every column up to H then starting again underneath with empty cell row 13, then data 14 - 24 until data runs out). I need to automatically find the picture relating to the information in row 3 on my sheet. ie. the picture will be saved under the same name as the data stored in row 3 in a specific location. was thinking of using a macro but I'm not sure how i could get it to work. Any ideas? Sorry if this is not clear!
Re: How to insert pictures into Excel automatically ?
Hi guys,
I searched the web for a function to automatic insert and size the picture.
I have found this code
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
With TargetCells
t = .Top + ((.Columns.Count - 1) * 37.5)
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
Top = .Top
MsgBox ("t = " & t & vbCrLf & "top = " & Top)
End With
' position picture
With p
.Top = t
.Left = l + 2
'.Width = w - 2
'.Height = h - 2
'.Placement = xlMoveAndSize
.ShapeRange.ScaleWidth 0.32, msoFalse, msoScaleFromTopLeft
.ShapeRange.ScaleHeight 0.32, msoFalse, msoScaleFromTopLeft
End With
Set p = Nothing
End Sub
'Placement = xlMoveAndSize
Wich works pretty well besides the fact that as i go down in Line the picture gets more and more out of the cell.
I tried to solve it adjusting the .top but it isnt working.
Anyone can help?
Re: How to insert pictures into Excel automatically ?
Hi,
Try this code :
Code:
Sub TestInsertPicture()
InsertPicture "C:\FolderName\PictureFileName.gif", _
Range("D10"), True, True
End Sub
Sub InsertPicture(PictureFileName As String, TargetCell As Range, _
CenterH As Boolean, CenterV As Boolean)
' inserts a picture at the top left position of TargetCell
' the picture can be centered horizontally and/or vertically
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCell
t = .Top
l = .Left
If CenterH Then
w = .Offset(0, 1).Left - .Left
l = l + w / 2 - p.Width / 2
If l < 1 Then l = 1
End If
If CenterV Then
h = .Offset(1, 0).Top - .Top
t = t + h / 2 - p.Height / 2
If t < 1 Then t = 1
End If
End With
' position picture
With p
.Top = t
.Left = l
End With
Set p = Nothing
End Sub
Re: How to insert pictures into Excel automatically ?
Hi,
thanks for the reply but i have already tried that code to.
Yesterday i decided to test my code on another worksheet and it works perfectly! The problem seems to be the format of the worksheet. But i need to maintain that format because it's for printing.
The same situation happens if u zoom, at 100% the pictures are inserted in the right spot (normal worksheet) and with zoom they don't.
Anyone has a clue how to counter that?
Thanks in advance
Re: How to insert pictures into Excel automatically ?
This code works in Excel 2007:
Code:
Dim imgIcon
With Range("A1")
Set imgIcon = ActiveSheet.Shapes.AddPicture(Filename:="C:\mypicture.jpg", LinkToFile:=False, SaveWithDocument:=True, Left:=.Left, Top:=.Top, Width:=30, Height:=30)
End With
Set imgIcon = Nothing
ALSO SEE THE USEFUL LINKS BELOW: