COM Server - Insert Inline Shapes in table

1 次查看(过去 30 天)
I have two figures which should be inserted into a two column table in word. In the code below, I check to make sure the filenames exist, move the selection to the correct table, and select the first cell in the first column then insert the image there. In the second section, I move the selection to the second column of the same table, which appear to work. However the second image is the inserted into the first column of the table. The selection also appears to move? Why is the second image not inserted in the correct selection?
%
if sum(ind_fv) == 1
actxWord.ActiveDocument.Tables.Item(4).Cell(1,1).Range.Select;
actxWord.Application.Selection.InlineShapes.AddPicture(...
fullfile(images(ind_fv).folder,images(ind_fv).name),0,1);
end
%
if sum(ind_ad) == 1
actxWord.ActiveDocument.Tables.Item(4).Cell(1,2).Range.Select;
actxWord.Application.Selection.InlineShapes.AddPicture(...
fullfile(images(ind_ad).folder,images(ind_ad).name),0,1);
end

回答(1 个)

Aditya
Aditya 2023-8-24
编辑:Aditya 2023-8-24
Hello Cory Snyder,
In the context of your code, using Range.Select alone does not move the insertion point to the desired cell for image insertion. It only selects the range without changing the cursor position.
To properly insert an image into a specific cell, you need to directly access the range of the cell and then use the InlineShapes.AddPicture method to insert the image into that range.
In your case you need to update your code like this:
cell1 = actxWord.ActiveDocument.Tables.Item(4).Cell(1,1)
cell1.Range.InlineShapes.AddPicture(image,0,1);
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by