PDA

View Full Version : inserting pictures thru VBA



colene
11-18-2008, 08:35 AM
The following code works good and inserts the picture at the location for the path specified when executed locally ..

While doing so on servers(TOMCAT) it gives an error on this line saying insert cannot cannot happen ..though the path is correct
ActiveSheet.Pictures.Insert(path).Select


Private Sub Worksheet_Activate()
Dim path As String
path = ActiveSheet.Range("A66").Value
Range("F1:I1").Select ActiveSheet.Pictures.Insert(path).Select
Selection.ShapeRange.IncrementLeft 90.4
Selection.ShapeRange.IncrementTop 4.8
Range("A1").Select
End Sub

Krishna Kumar
11-18-2008, 09:15 AM
Try this UDF from Damon.

Function ShowPicD(PicFile As String) As Boolean
'By Damon Ostrander
'http://www.mrexcel.com/forum/showthread.php?t=100737&
Dim AC As Range
Static P As Shape
On Error GoTo Done
Set AC = Application.Caller
If PicExists(P) Then
P.Delete
Else
'look for a picture already over cell
For Each P In ActiveSheet.Shapes
If P.Type = msoLinkedPicture Then
If P.Left >= AC.Left And P.Left < AC.Left + AC.Width Then
If P.Top >= AC.Top And P.Top < AC.Top + AC.Height Then
P.Delete
Exit For
End If
End If
End If
Next P
End If
Set P = ActiveSheet.Shapes.AddPicture(PicFile, True, True, AC.Left, AC.Top, 200, 200)
ShowPicD = True
Exit Function
Done:
ShowPicD = False
End Function
Function PicExists(P As Shape) As Boolean
'Return true if P references an existing shape
Dim ShapeName As String
On Error GoTo NoPic
If P Is Nothing Then GoTo NoPic
ShapeName = P.Name
PicExists = True
NoPic:
PicExists = False
End Function

and use the formula in a cell

=ShowPicD("YourPath\MyPicture.jpg")

HTH