Select Two Images In Excel Using ActiveX

조회 수: 2 (최근 30일)
Michael Cappello
Michael Cappello 2023년 6월 8일
댓글: Michael Cappello 2023년 7월 28일
I have an Excel Workbook which contains two pictures in it. The picture names are "Picture 2" & "Picture 3".
I am able to select either picture by itself like so
E = actxserver('Excel.Application'); % start COM server
E.Visible=1; % makes visible
E.Workbooks.Open('myWorkbook.xlsx'); % opens file
E.ActiveSheet.Shapes.('Picture 2').Select % selects Picture 2
or alternatively
E.ActiveSheet.Shapes.Item(2).Select
I cannot figure out how to select both at once. I tried several different ways but none of them work
E.ActiveSheet.Shapes.('Picture 2','Picture 3').Select
E.ActiveSheet.Shapes.({'Picture 2','Picture 3'}).Select
E.ActiveSheet.Shapes.("Picture 2","Picture 3").Select
E.ActiveSheet.Shapes.(["Picture 2","Picture 3"]).Select
I recorded a macro in Excel selecting the two pictures...
ActiveSheet.Shapes.Range(Array("Picture 2", "Picture 3")).Select
...and notice that the two picture names are grouped in the Array function.
Thanks in advance for any help.
Mike

답변 (1개)

Prathamesh
Prathamesh 2023년 7월 21일
Hi,
I understand that you want to select two images from excel.
You can use the cellfun()” function to create imagenamecell” object and use it to select multiple images.
Excel = actxserver('Excel.Application');
fullFileName = 'C:\path\to\workbook.xlsx';
ExcelWorkbook = Excel.Workbooks.Open(fullFileName);
oSheet = ExcelWorkbook.Sheets.Item(1);
Shapes = oSheet.Shapes;
imageNames = {'Picture 2', 'Picture 3'};
imageNamesCell = cellfun(@(x) [oSheet.Name, '!', x], imageNames, 'UniformOutput', false);
Shapes.Select(imageNamesCell);
Alternatively, you can use a for loop
Excel = actxserver('Excel.Application');
fullFileName = 'C:\path\to\workbook.xlsx';
ExcelWorkbook = Excel.Workbooks.Open(fullFileName);
oSheet = ExcelWorkbook.Sheets.Item(1);
Shapes = oSheet.Shapes;
imageNames = {'Picture 2', 'Picture 3'};
for i = 1:numel(imageNames)
shapeName = imageNames{i};
shape = Shapes.Item(shapeName);
shape.Select;
end
I hope this resolves your issue.
  댓글 수: 2
Michael Cappello
Michael Cappello 2023년 7월 28일
Hi Prathamesh,
Thank you for taking the time to respond. Unfortunately neither method works for me.
When I try to select them all via "Shapes.Select(imageNamesCell);" I get the following error message: "Incorrect number or types of inputs or outputs for function 'Select'."
When I tried the second method, the i'th picture does get selected, but all other pictures are deselected.
Do you know why either of these problems would occur?
I am running Matlab 2022b on Windows 10 using Microsoft® Excel® for Microsoft 365 MSO (Version 2305 Build 16.0.16501.20074) 32-bit.
Michael Cappello
Michael Cappello 2023년 7월 28일
After some more digging I found this: https://learn.microsoft.com/en-us/office/vba/api/excel.shape.select which helped me to get the second method to work, I changed the line:
from: shape.Select;
from: shape.Select(false);
The argument to shape.Select should be set to true (evidently the default value) to replace the current selection with the specified object. False to extend the current selection to include any previously selected objects and the specified object.
I would still like to know how to select multiple pictures at once.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 ActiveX에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by