How to generate a figure caption in Word with Table of Figures Reference in the caption?
41 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
ProblemSolver
2023-6-27
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 个评论
Youssef Telha
2024-4-19
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!