Search code examples
vbaimageautomationcatia

Select a range in Excel to paste a picture from CATIA v5 VBA


I would like to add pictures in Excel in specifics cells using a CATIA macro. Unfortunately, I can't achieve this, sometime my code works fine and sometime it crashes at the "select" line.

(For information, pictures are saved in a folder then I insert it in the sheet and then I want to put them in the right cell, this is where I have an issue.)

Here it is :

'DEFINE WHERE TO PASTE PICTURE AND PASTE
wb_BOM.Sheets(1).Shapes(i).Height = 56
wb_BOM.Sheets(1).Shapes(i).Cut
wb_BOM.Sheets(1).Range("E1").select
wb_BOM.Sheets(1).Paste

I also try without SELECT statement like this :

wb_BOM.Sheets(1).Paste Destination:=Range("E1")

But it also crashed... Do not hesitate if you have any idea ! Thanks !

  • EDIT :

Sorry I forgot the error : The select method from range class has failed.

Error


Solution

  • If you're not copying between worksheets you can try this code:

    'DEFINE WHERE TO PASTE PICTURE AND PASTE
    Sheets(1).Shapes(1).Height = 56
    Dim targetcell As Range
    Set targetcell = Sheets(1).Range("E1")
    Sheets(1).Shapes(1).Top = targetcell.Top
    Sheets(1).Shapes(1).Left = targetcell.Left
    

    But I tried your code (version with select) and it works perfectly for me.