COM Server - Insert Inline Shapes in table

9 vues (au cours des 30 derniers jours)
Cory Snyder
Cory Snyder le 2 Août 2023
Modifié(e) : Aditya le 24 Août 2023
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

Réponses (1)

Aditya
Aditya le 24 Août 2023
Modifié(e) : Aditya le 24 Août 2023
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!

Catégories

En savoir plus sur Scripts dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by