Filter löschen
Filter löschen

How to select an Excel shape in Matlab using ActiveX?

9 Ansichten (letzte 30 Tage)
Rakeshkumar Karunakaran
Rakeshkumar Karunakaran am 18 Jan. 2021
Beantwortet: Divyanshu am 29 Feb. 2024
Hi all,
I am trying to select a picture (named 'Picture 3') that I pasted on Excel. The goal is to eventually make multiple copies of the picture within Excel using MATLAB. I am basing my code on a macro I recorded on Excel to paste the picture several times. However, when I try to execute this code on MATLAB, I get the error 'One or more output arguments not assigned during call to "Select".' I don't understand what am I missing here. Can you please help? My code is as follows:
objExcel = actxserver('Excel.Application');
objExcel.Visible = true;
% Open the workbook we want to paste the image onto.
excelWorkbook = objExcel.Workbooks.Open(fullFileName); % Full path is necessary!
excelSheet = objExcel.ActiveSheet;
Shapes = excelSheet.Shapes;
Picture = Shapes.Range('Picture 5');
PictureSelect = Picture.Select;
Thank you!

Antworten (1)

Divyanshu
Divyanshu am 29 Feb. 2024
Hi Rakeshkumar,
The error is probably due to the following line of code:
PictureSelect = Picture.Select;
Because this error is generally thrown when the called function returns nothing and you are assigning the returned value to some variable.
You can refer the following sample code which copies the image pasted in the excel sheet and creates multiple replicas of the same:
numCopies = 5;
ExcelApp = actxserver('Excel.Application');
ExcelApp.Visible = true;
Workbook = ExcelApp.Workbooks.Open('<Full-Path to your excel file>');
Worksheet = Workbook.Sheets.Item(1);
ShapeToCopy = Worksheet.Shapes.Item(1);
% Copy the image
ShapeToCopy.Copy();
% Paste the image copies at desired locations
for i = 1:numCopies
% Paste the image
Worksheet.Paste();
% Access the pasted shape (the last shape in the collection)
PastedShape = Worksheet.Shapes.Item(Worksheet.Shapes.Count);
PastedShape.Left = PastedShape.Left + (PastedShape.Width+2);
PastedShape.Top = PastedShape.Top;
end
% Save the workbook if needed
Workbook.Save();
% Close the workbook and quit Excel if you're done
Workbook.Close();
ExcelApp.Quit();
% Release the ActiveX objects
delete(ExcelApp);
Hope it helps!

Community Treasure Hunt

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

Start Hunting!

Translated by