Consulting

Results 1 to 2 of 2

Thread: inserting pictures thru VBA

  1. #1
    VBAX Regular
    Joined
    Apr 2008
    Posts
    27
    Location

    inserting pictures thru VBA

    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

    [VBA]
    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
    [/VBA]

  2. #2
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Try this UDF from Damon.

    [vba]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[/vba]

    and use the formula in a cell

    =ShowPicD("YourPath\MyPicture.jpg")

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •