How to generate a figure caption in Word with Table of Figures Reference in the caption?

41 ビュー (過去 30 日間)
Katrina
Katrina 2023 年 5 月 26 日
コメント済み: Youssef Telha 2024 年 4 月 19 日
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!

回答 (1 件)

ProblemSolver
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
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 ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by