Replacing an image in excel

5 views (last 30 days)
Sven Henrich
Sven Henrich on 7 Sep 2018
Commented: Sven Henrich on 7 Sep 2018
Hello everyone, I know how to write an image to a specific location/cell in Excel using Matlab, but I am having a really hard time finding or building a script that can exchange a specific image on a specific sheet.
Here's the first bit of code:
%Start Active X Server
xlApp = actxserver('Excel.Application');
xlApp.visible = 1;
%Open the the spreadsheet
xlworkbook = xlApp.Workbooks.Open([pwd,'\New.xls']);
%Select sheet:
Sheet = get(xlworkbook.Sheets,'Item',7); % sheet
Sheet.Select;
%Work in Sheet
xlsheet = xlworkbook.ActiveSheet;
xlsheet.Shapes.Item(1).Select
As can be seen, using the Activex commands, I was able to get as far as selecting the specific shape on a sheet, but after that I'm stuck, as I can't find any proper documentation on how to replace the selected image (i.e. .shape).
Thanks a lot!
  2 Comments
Kevin Chng
Kevin Chng on 7 Sep 2018
I'm not sure. Do you mind try this?
xlsheet.Shapes.Fill.UserPicture ("C:\image.png")
I'm not sure. however, sometimes, i will take this link as guideline.
Sven Henrich
Sven Henrich on 7 Sep 2018
Thank you for your response.
Unfortunately Matlab/ActiveX calls an undefined function or variable error for that code for the .Fill.UserPicture bit.
Everywhere I look, people are deleting the old images and adding new ones, but it can't be that hard to exchange an image path. Especially when I need to replace the pictures which have no cell characteristics (i.e. are "free floating" on a sheet).

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 7 Sep 2018
First, avoids relying on ActiveAnything, it's a recipe for bugs. For example, with your code something (e.g. the user) could activate a different sheet between the moment you activate the 7th sheet and the moment you retrieve the active sheet. In your case, it's also a complete waste of time since, assuming that the active sheet doesn't change, what your code is doing is simply
xlsheet = Sheet; %in a roundabout way
With regards to your question, to replace a picture you have to delete the existing shape and create a new one.
xlApp = actxserver('Excel.Application');
xlApp.Visible = true; %optional
xlworkbook = xlApp.Workbooks.Open(fullfile(pwd, 'New.xls')); %prefer fullfile to building the path yourself
xlSheet = xlworkbook.Sheets.Item(7); %or xlSheet = get(xlworkbook.Sheets,'Item',7);
oldshape = xlSheet.Shapes.Item(1);
newshape = xlSheet.Shapes.AddPicture(picturefile, 0, 1, oldshape.Left, oldshape.Top, oldshape.Width, oldshape.Height);
oldshape.Delete;
xlworkbook.Close(true); %close and save changes
xlApp.Quit;
  1 Comment
Sven Henrich
Sven Henrich on 7 Sep 2018
Thank you for the reply. Yes using ActiveX should be done with care. The roundabout is because I have multiple named sheets which I call in my program via that line and just simplified it for the question.
Anyhow, thank you for your help I finally saw what I was doing wrong.
Cheers!

Sign in to comment.

More Answers (0)

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!