Modify Chart in Word with actxserver

Hello everyone, I'm working on a word template file including chart that have been included from Excel. I'm willing to modify those charts. For now I'm able to find the corresponding chart using the caption. But whenever I modify the chart, it only show 1 point in (0,0) instead of the curve that was shown before.
I would appreciate any help on this !
Thanks in advance
WordApplication=actxserver('Word.Application')
Documents = WordApplication.Documents;
Documents.Open(fullSavefile);
Doc = Documents.Item(Documents.Count);
update_word_chart_by_caption(Doc, 'Caption Title',X,Y);
function update_word_chart_by_caption(Doc, captionText, xValues, yValues, warnings)
chartObj = find_chart_by_caption(Doc, captionText);
Chart = chartObj.Chart;
xValues = double(xValues(:))';
yValues = double(yValues(:))';
Series = Chart.SeriesCollection.Item(1);
Series.XValues = xValues;
Series.Values = yValues;
Chart.Refresh;
end
function chartObj = find_chart_by_caption(Doc, captionText)
chartObj = [];
Text_Margin_Search = 50; %Search in the following 50 characters
for i = 1:Doc.InlineShapes.Count
Item = Doc.InlineShapes.Item(i);
if Item.HasChart
afterStart = Item.Range.End;
afterEnd = min(Item.Range.End + Text_Margin_Search, Doc.Content.End);
txtAfter = char(Doc.Range(afterStart, afterEnd).Text);
aroundText = normalize_text(txtAfter);
if contains(aroundText, target)
chartObj = Item;
return;
end
end
end

2 commentaires

Torsten
Torsten le 5 Août 2026 à 13:29
Why do you try to do this in MATLAB and not directly in Word ?
VINCENT
VINCENT il y a environ 10 heures
I have to do it many times (~100 times) and the X and Y values come from calculations, so it would be longer to save them as xlsx files and plot them.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2018b

Question posée :

le 5 Août 2026 à 8:08

Commenté :

le 6 Août 2026 à 11:54

Community Treasure Hunt

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

Start Hunting!

Translated by