How to order a Shapes object with actxserver for Excel?

I am trying to send an image I input into an Excel file to the back of the Shapes object. I have tried two different methods.
Method 1: Selection and Send to Back
objExcel = actxserver('Excel.Application');
objExcel.Workbooks.Open(excelFileName);
myshapes = objExcel.ActiveSheet.Shapes;
N = myshapes.Count;
ShapeToBack = myshapes.Item(N);
ShapeToBack.SendToBack();
The problem is that the .SendToBack function is not recognized. I did test out the Send To Back option in Excel and this did reorder the image in the myshapes. I was hoping to do that programatically, but I can't find a way to do it in MATLAB.
Method 2: Copy Delete and Paste
objExcel = actxserver('Excel.Application');
objExcel.Workbooks.Open(excelFileName);
myshapes = objExcel.ActiveSheet.Shapes;
N = myshapes.Count;
for j = 1:N-1
ShapeToCopy = myshapes.Item(j);
ShapeToCopy.Copy();
myshapes.Item(j).Delete
objExcel.ActiveSheet.Paste();
end
The problem here is that for some reason this doesn't change the order. The shape object at the end of myshapes stays at the end.

2 Comments

The problem isn't in MATLAB, there's no SendtoBack method for the Excel shapes object -- see <Excel shapes VBA methods>. There is a <ShapeRrange object ZOrder method>, however.
I've done quite a lot w/ ActiveX in Excel from MATLAB but have never used shapes in Excel so absolutely no experience in controlling them, but if it's in the VBA doc, you can (eventually) find a way from MATLAB. You have to remember that MATLAB doesn't know anything about VBA syntax, however, so much of the higher level coding in the examples won't work directly.
Thank you for pointing me towards the ShapeRange object, it helped me find the answer.

Sign in to comment.

 Accepted Answer

I was able to find a way to send the top shape to the back in MATLAB using ActiveX.
objExcel = actxserver('Excel.Application');
objExcel.Workbooks.Open(excelFileName);
myshapes = objExcel.ActiveSheet.Shapes;
N = myshapes.Count;
myshapes.Item(N).Select;
objExcel.Selection.ShapeRange.ZOrder('msoSendToBack')
objExcel.Quit;

More Answers (0)

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!