0

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

Macronaute
  • 184
  • 3
  • 12
  • You can't `Select` the range if the worksheet isn't active. – BigBen Oct 22 '20 at 13:47
  • `wb_BOM.Sheets(1).Paste Destination:=wb_BOM.Sheets(1).Range("E1")` perhaps (untested). – BigBen Oct 22 '20 at 13:47
  • Activate and select make errors when used with catia in this case, and I don't know why, yes I already tried it whit workbook and worksheet precision but it didn't worked as well. Thanks anyway for your time ! – Macronaute Oct 22 '20 at 14:38

1 Answers1

1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • If the question is unclear then please do not answer it. – Dharman Oct 22 '20 at 13:36
  • Thanks a lot @salamander Krajza, it works fine ! Sorry if I wasn't clear but I don't really understand why it works from Excel app but not from Catia... So not so easy to explain. – Macronaute Oct 22 '20 at 14:41