none
Insert a Picture in particular Cell in Excel

    Question

  • Helo,

     

    I want to insert picture in particular cell of excel file. And also I need to resize the picture according to size of particular cell- width & height

     

    Is it possible to do from our VFP9?

     

    loexcel.Worksheets("PaySlip").Cells(wh,1).insert.picture ????????????

     

    Thanks

    Thursday, January 29, 2009 1:16 PM

Answers

  • You can insert and size it to a cell or a group of cells (a range IOW). ie:

    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  
     
    • Marked as answer by AJEETH Saturday, January 31, 2009 2:22 PM
    Saturday, January 31, 2009 12:45 PM
  • AJEETH said:

    I want to insert picture in particular cell of excel file. And also I need to resize the picture according to size of particular cell- width & height

    Is it possible to do from our VFP9?

    Thanks



    Here is some sample code:

     

    oXL = CREATEOBJECT( 'Excel.Application' )

    oWB = oXL.Workbooks.Add()

    oSheet = oWB.ActiveSheet

    oSheet.Range("A1").Select

    oSheet.Pictures.Insert("<< full path to image file >>").Select()

    oXL.Visible = .T.

     

    Both Word and Excel have a macro recorder that allows us to record our keystrokes and translate them into VBA code. This can provide us with another way to begin figuring out how to automate a given task. All we need to do is record a macro while performing the task interactively and examine the generated code in the macro editor afterwards.

     

    And here are the steps that you can use to translate the VBA into VFP:

    1.      Add parentheses around the parameter list

    2.      Examine the calling prototype for the method in the object browser and rearrange the parameters in the generated macro so that they appear in the same order that they are listed here. Because VBA uses named parameters, their order does not matter. This is not so in VFP where parameters are positional. Regardless of the order of the parameters in the VBA macro, the Object Browser and IntelliSense always display them in the order required for correct Visual FoxPro syntax.

    3.      Remove the names of the parameters on the left side of the ‘:=’ as well as the ‘:=’

    4.      Replace the named constants with their values. This includes replacing True with 1 and False with 0. Note: An alternative would have been to add #DEFINE statements at the top of the method like this after looking up the values for the named constants in the Object Browser:

          #DEFINE TRUE 1

    #DEFINE FALSE 0

          #DEFINE WDOPENFORMATAUTO 0


    Marcia G. Akins
    • Marked as answer by AJEETH Saturday, January 31, 2009 2:22 PM
    Saturday, January 31, 2009 12:00 PM

All replies

  • In this context, a picture is an OLEObject. I don't know whether Excel lets you put an OLEObject in a particular cell. You should experiment with that interactively.

    Tamar
    Thursday, January 29, 2009 9:20 PM
    Answerer
  • AJEETH said:

    I want to insert picture in particular cell of excel file. And also I need to resize the picture according to size of particular cell- width & height

    Is it possible to do from our VFP9?

    Thanks



    Here is some sample code:

     

    oXL = CREATEOBJECT( 'Excel.Application' )

    oWB = oXL.Workbooks.Add()

    oSheet = oWB.ActiveSheet

    oSheet.Range("A1").Select

    oSheet.Pictures.Insert("<< full path to image file >>").Select()

    oXL.Visible = .T.

     

    Both Word and Excel have a macro recorder that allows us to record our keystrokes and translate them into VBA code. This can provide us with another way to begin figuring out how to automate a given task. All we need to do is record a macro while performing the task interactively and examine the generated code in the macro editor afterwards.

     

    And here are the steps that you can use to translate the VBA into VFP:

    1.      Add parentheses around the parameter list

    2.      Examine the calling prototype for the method in the object browser and rearrange the parameters in the generated macro so that they appear in the same order that they are listed here. Because VBA uses named parameters, their order does not matter. This is not so in VFP where parameters are positional. Regardless of the order of the parameters in the VBA macro, the Object Browser and IntelliSense always display them in the order required for correct Visual FoxPro syntax.

    3.      Remove the names of the parameters on the left side of the ‘:=’ as well as the ‘:=’

    4.      Replace the named constants with their values. This includes replacing True with 1 and False with 0. Note: An alternative would have been to add #DEFINE statements at the top of the method like this after looking up the values for the named constants in the Object Browser:

          #DEFINE TRUE 1

    #DEFINE FALSE 0

          #DEFINE WDOPENFORMATAUTO 0


    Marcia G. Akins
    • Marked as answer by AJEETH Saturday, January 31, 2009 2:22 PM
    Saturday, January 31, 2009 12:00 PM
  • You can insert and size it to a cell or a group of cells (a range IOW). ie:

    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  
     
    • Marked as answer by AJEETH Saturday, January 31, 2009 2:22 PM
    Saturday, January 31, 2009 12:45 PM
  • Really Very Nice Guidences Available With This  VFP Forums
    Trying To Upgrade
    Saturday, January 31, 2009 2:24 PM

  • Here is some sample code:

    oXL = CREATEOBJECT( 'Excel.Application' )

    oWB = oXL.Workbooks.Add()

    oSheet = oWB.ActiveSheet

    oSheet.Range("A1").Select

    oSheet.Pictures.Insert("<< full path to image file >>").Select()

    oXL.Visible = .T.

    Marcia G. Akins

    Just a note:

    The command   "oSheet.Pictures.Insert("<< full path to image file >>").Select()"  causes an OLE Error saying (translated) "Cannot obtain Insert property of the Pictures class" in my Excel 2003 and VFP9 when the file name does not contain path and Excel cannot find it in its default folder.

    The code from Cetin reports correct error message.

    Thursday, September 06, 2012 10:32 AM
    Moderator
  • Hi Marcia, I am first time user to the forum and sure hoping you can help me out.  I need to do

    this exact same activity.  But I have two questions:  1) I am not sure if your code is repeating the specific file names (which in my case I have 1109 files) and 2)  where are suggesting to put the format picture inches in this
    code.  I didn't quite follow.

    Also must I copy the code into my spreadsheet and don't enter that into the macro editor ?  Then I presume

    you must save this file as an *.xlsm -- correct ?

    Thanks for any light you can shed on this discussion.

    Sincerely Mercedes

    Monday, November 26, 2012 1:06 PM
  • Im sorry I think my question should have been directed to Ajeeth and not Marcia.  Sorry for the faux paus.

    Mercedes

    Monday, November 26, 2012 1:08 PM
  • The code Ajeeth quoted (that Marcia wrote) is VFP code that automates Excel. It doesn't go into a spreadsheet; you run it from your VFP application and it opens Excel and tells it what to do.

    Tamar

    Monday, November 26, 2012 10:00 PM
    Answerer