필터 지우기
필터 지우기

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

조회 수: 38 (최근 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!

답변 (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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by