How to insert an image into a specific Excel cell?

48 views (last 30 days)
DDC
DDC on 20 Sep 2024
Edited: dpb on 25 Sep 2024
In Excel, I can place an image into a specific cell using Insert -> Pictures -> Place in Cell; this inserts the image into the cell, not floating over the cell. Can the same thing be done from MATLAB using ActiveX? Note I already have working code to insert a floating image in any desired location. What I really need to do is to insert it into a cell of my choosing.

Answers (1)

dpb
dpb on 20 Sep 2024
Moved: dpb on 20 Sep 2024
Record a macro doing the desired operation and then look up the functions/methods referenced to in it in the <VBA for Excel documentation>; it will show you all the details on syntax.
Converting VBA to ActiveX in a form for MATLAB syntax is an exercise in replacing higher-level VBA constructions like using block constructs, etc., with specific calls from object handles that MATLAB can recognize, but it can be done with enough patience. Be prepared to restart Excel and kill crashed processes; setting a breakpoint and executing proposed tries from the command line before letting the code crash on first attempts can alleviate much grief, or putting each line in a try...catch block with breakpoints in the catch side although that can get tedious. Remember to close the workbook and the ActiveX to not leave zombie processes hanging around...but, it's almost guaranteed you'll crash it many times before getting something complicated to work...
  4 Comments
DDC
DDC on 24 Sep 2024
I tried this and several other permutations, keep getting "Dot indexing is not supported for variables of this type." I found a workaround that gets me to the desired end. Rather that inserting an image file into a cell, I use imread to create a MATLAB image from the file, print to copy it to the clipboard, then
excelSheet.Range(xlcell).PasteSpecial();
excelSheet.Shapes.Item(1).PlacePictureInCell();
to insert the figure into Excel. The figure is actual in the cell and not just floating over the cell.
dpb
dpb on 24 Sep 2024
Edited: dpb on 25 Sep 2024
Did the VBA macro exercise actually do what was wanted as well?
I've never tried to do such a thing even in Excel itself, didn't really know it could be done, so no experience and don't have time to go try at the moment...well, it doesn't take long to at least poke at it...
K>> FF=fullfile('C:\Users\Duane\Documents\Book1.xlsm');
K>> excel=actxserver('Excel.Application');
K>> wbk=excel.Workbooks.Open(FF)
wbk =
Interface.000208DA_0000_0000_C000_000000000046
K>> wksht=wbk.ActiveSheet
wksht =
Interface.000208D8_0000_0000_C000_000000000046
K>> wksht.Name
ans =
'Sheet1'
K>> range=wksht.Range('A1')
range =
Interface.00020846_0000_0000_C000_000000000046
K>> range.Select
ans =
logical
1
K>> wksht.Range('A1').Select % can use double dot addressing here, too
ans =
logical
1
K>> range.InsertPictureInCell % above returned by tab completion at command line for range
The above discovered that the range object thinks there is a method named InsertPictureInCell, but the range object methods listed in the VBA for Excel documentation don't show it...I don't recall ever running into that before...anyways, proceeding on following the macro outline--
I could not find documentation on the method/function other than a reference to a user-written function of the same name in C#/.Net syntax that illustrated lower level methods to do the work...but it has a different calling syntax including the cell row, column, as initial parameters. End result is I am not sure from where the reference came, but it showed up here as working precisely as the macro code suggests it should--carrying on from above:
K>> p=pwd;
K>> range.InsertPictureInCell(fullfile(p,'area.jpg'))
K>> excel.ActiveWorkbook.Save % write the updated file
K>> winopen(FF) % open it and see if it worked...indeed, it did!!!
K>> excel.ActiveWorkbook.Close(0) % clean up...
K>> excel.Quit; delete(excel);clear excel
actually put the jpg image in the spreadsheet here...
On my earlier comment in response, I had forgotten that the .Select method returns a logical indicating success/failure and not the object handle so that was wrong, sorry.

Sign in to comment.

Categories

Find more on Data Export to MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!