How to add a caption to a picture imported in ms word document using actxserver?

8 views (last 30 days)
Hi peeps,
the word document is already created,picture is allready imported. How can I add a caption to this picture. I sow the interface: Application of the pictureHandle object, the way I used it doesn't work. there is my code:
function msWordGenerator(wordFileName, sourcePath)
% WORDGENERATOR Summary of this function goes here
% generate a world document as part of the verification process. Insert test report pictures.
% save the .doc in the source path
%
% Detailed explanation goes here
% inputparameter wordFileName -> type string -> filename of the .doc
% inputparameter sourcePath -> type string -> path of the folder with the
% jpegs / output destination for saving the doc
% Restrictions: it works only for 'jpeg' data format
%% Initialization
fileSpec = fullfile(sourcePath,wordFileName);
[actXWord, wordHandle] = startWord(fileSpec);
fprintf('Document will be saved in %s\n', fileSpec);
wordPageNumbers(actXWord, 'wdAlignPageNumberRight');
%% filter the jpeg files only
cd(sourcePath);
fileList = dir('*.jpeg');
filePathLength = length(fileList);
%% insert the jpegs in the .doc
for i = 1:filePathLength
pictureHandle = wordHandle.InlineShapes.AddPicture([pwd '/' fileList(i).name]);
pictureHandle.Application.Caption = 'testCaption';
if i == 3
% new page vor 'V' graphs
invoke(actXWord.Selection, 'MoveStart', 6);
invoke(actXWord.Selection,'InsertBreak');
end
end
%% close with save
closeWord(actXWord, wordHandle, fileSpec);
end
%% Help functions for open, page numbering, close and save the .doc
function [actxWord,wordHandle] = startWord(wordFilePath)
% Start an ActiveX session with Word:
actxWord = actxserver('Word.Application');
actxWord.Visible = true;
trace(actxWord.Visible);
if ~exist(wordFilePath,'file')
% Create new document:
wordHandle = invoke(actxWord.Documents,'Add');
else
% Open existing document:
wordHandle = invoke(actxWord.Documents,'Open',wordFilePath);
end
end
function closeWord(actxWordPointer,wordHandlePointer,wordFilePath)
if ~exist(wordFilePath,'file')
% Save file as new:
invoke(wordHandlePointer,'SaveAs',wordFilePath,1);
else
% Save existing file:
invoke(wordHandlePointer,'Save');
end
% Close the word window:
invoke(wordHandlePointer,'Close');
% Quit MS Word
invoke(actxWordPointer,'Quit');
% Close Word and terminate ActiveX:
delete(actxWordPointer);
end
function wordPageNumbers(actxWordPointer,alignPointer)
%make sure the window isn't split
if (~strcmp(actxWordPointer.ActiveWindow.View.SplitSpecial,'wdPaneNone'))
actxWordPointer.Panes(2).Close;
end
%make sure we are in printview
if (strcmp(actxWordPointer.ActiveWindow.ActivePane.View.Type,'wdNormalView') || ...
strcmp(actxWordPointer.ActiveWindow.ActivePane.View.Type,'wdOutlineView'))
actxWordPointer.ActiveWindow.ActivePane.View.Type ='wdPrintView';
end
%view the headers-footers
actxWordPointer.ActiveWindow.ActivePane.View.SeekView='wdSeekCurrentPageHeader';
if actxWordPointer.Selection.HeaderFooter.IsHeader
actxWordPointer.ActiveWindow.ActivePane.View.SeekView='wdSeekCurrentPageFooter';
else
actxWordPointer.ActiveWindow.ActivePane.View.SeekView='wdSeekCurrentPageHeader';
end
%now add the pagenumbers 0->don't display any pagenumber on first page
actxWordPointer.Selection.HeaderFooter.PageNumbers.Add(alignPointer,0);
actxWordPointer.ActiveWindow.ActivePane.View.SeekView='wdSeekMainDocument';
end
Does anybody have an idea?
Much 10x in advance,
GR

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!