How to insert an image into a specific Excel cell?
48 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
dpb
2024-9-20
移动:dpb
2024-9-20
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 个评论
dpb
2024-9-24
编辑:dpb
2024-9-25
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Export to MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!