Insert Picture in a specific cell of an excel using vb6

Asked By Jan S
08-Feb-10 01:10 AM
Earn up to 0 extra points for answering this tough question.

Hi,

I need to insert a picture from a local path into a specific cell (For e.g. "A2") in an excel?

can anybody tell me how to proceed with this?

 

Thanks

Jan

 

 

 

 

 

 

  re: Insert Picture in a specific cell of an excel using vb6

Ryan C. replied to Jan S
08-Feb-10 02:45 AM
Try this:

Private Sub CommandButton4_Click()
    Dim PicLocation As String
    Dim MyRange As String
    
    ActiveSheet.Unprotect
    Range("A9").Select
    MyRange = Selection.Address

    PicLocation = Application.GetSaveAsFilename("C:\", "Image Files (*.jpg),*.jpg", , "Specify Image Location")
    
    If PicLocation <> "False" Then
        ActiveSheet.Pictures.Insert(PicLocation).Select
    Else
        Exit Sub
    End If
    
    With Selection.ShapeRange
        .LockAspectRatio = msoTrue
        If .Width > .Height Then
            .Width = Range(MyRange).Width
            If .Height > Range(MyRange).Height Then .Height = Range(MyRange).Height
        Else
            .Height = Range(MyRange).Height
            If .Width > Range(MyRange).Width Then .Width = Range(MyRange).Width
        End If
    End With
    
    With Selection
        .Placement = xlMoveAndSize
        .PrintObject = True
    End With
    
    Range("H16").Select
    ActiveSheet.Protect
End Sub

  You could use code snippet like this:-

web mavin replied to Jan S
08-Feb-10 04:13 AM
oExcel = Createobject('Excel.Application')  
With oExcel  
  .WorkBooks.Add  
  .Visible = .T.  
 
  With .ActiveWorkBook.ActiveSheet  
    Local loRange  
    loRange = .Range('B2:E6')  
 
    .Shapes.AddPicture( _samples+'data\graphics\buchstev.gif', .T., .T., ;  
      loRange.Left, loRange.Top, loRange.Width, loRange.Height)  
  Endwith  
Endwith  
(Refer this)

This would need the "Excel Interop" to be added as reference into your application.

Create New Account