How to generate a figure caption in Word with Table of Figures Reference in the caption?
36 views (last 30 days)
Show older comments
I'm trying to automate writing captions to a Word document with the structure "Figure: " + numberReference + "caption words..." to a Word document. I think I need to use the "InsertCrossReference", but I'm getting this error message:
Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: Command failed
Help File: wdmain11.chm
Help Context ID: 9066"
doc = actxserver('Word.Application');
doc.Selection.InsertCrossReference(0, 2, "xxx", true); % 0 = wdRefTypeNumberedItem, 2 = wdEntireCaption
I think that the VBA code would be similar to this:
With Selection
.InsertBefore "Figure "
.InsertCrossReference ReferenceType:=wdRefTypeNumberedItem, ReferenceKind:=wdEntireCaption, ReferenceItem:= 0
.InsertAfter Caption1
Many thanks in advance for your help!
0 Comments
Answers (1)
ProblemSolver
on 27 Jun 2023
The error you encountered while using the InsertCrossReference method in MATLAB's ActiveX interface for Word is likely due to incorrect parameter values or missing references. The error message you provided indicates that the command failed without providing specific details.
% Create a Word instance and make it visible
wordApp = actxserver('Word.Application');
wordApp.Visible = true;
% Add a new document
doc = wordApp.Documents.Add();
% Insert a caption with a numbered item
figureCaption = 'Caption words...';
selection = doc.Content;
selection.InsertCaption('Figure', figureCaption, [], 'wdCaptionPositionAbove');
% Get the reference to the inserted caption
captions = doc.Content.InlineShapes;
captionRange = captions.Item(1).Range;
% Insert the cross-reference
selection.Collapse(0); % Move the selection to the end of the document
selection.InsertAfter('Figure: ');
selection.Collapse(0); % Move the selection to the end of 'Figure: '
doc.Selection.InsertCrossReference('Figure', 'wdRefTypeNumberedItem', 'wdEntireCaption', captionRange, true);
% Clean up
doc.SaveAs('path_to_your_document.docx');
doc.Close();
wordApp.Quit();
the InsertCaption method is used to insert a caption with the specified text ('Caption words...') and caption label ('Figure'). The reference to the inserted caption is then obtained using the InlineShapes property. Finally, the InsertCrossReference method is called to insert the cross-reference, using the obtained caption range.
Make sure to adjust the file path in the SaveAs method to your desired location.
The specific error message you encountered could have other causes. It's also worth checking that your version of Microsoft Word is compatible with the version of MATLAB you are using.
1 Comment
Youssef Telha
on 19 Apr 2024
Hi,
I do have a question, I would like to learn all the possible command to manipulate a word document object such as insertCaption, InsertAfter etc. What is the main source or the documentation to find those.
Kind regards,
Youssef
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!