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 Comments

Why do you try to do this in MATLAB and not directly in Word ?
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.

Sign in to comment.

Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 5 Aug 2026 at 8:08

Commented:

about 6 hours ago

Community Treasure Hunt

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

Start Hunting!