clc;
fprintf('Beginning to run %s.m ...\n', mfilename);
close all;
clear;
workspace;
format long g;
format compact;
startingFolder = pwd;
if ~isfolder(startingFolder)
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.doc*');
[baseFileName, wordDocumentFolder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
return;
end
wordFullFileName = fullfile(wordDocumentFolder, baseFileName)
if ~isfile(wordFullFileName)
msgbox('Error: %s must exist already, and it does not.', wordFullFileName);
return;
end
imagesFolder = pwd;
fprintf('Now getting a list of image files in folder "%s".\n', imagesFolder);
filePattern = fullfile(imagesFolder, '*.png');
fileList = dir(filePattern);
if isempty(fileList)
[~, ~, ext] = fileparts(filePattern);
warningMessage = sprintf('No %s format images found in folder\n%s\n\nExiting.', ext, imagesFolder);
uiwait(warndlg(warningMessage));
return;
end
numberOfFiles = length(fileList);
fprintf('MATLAB is launching Word as an ActiveX server...\n');
Word = actxserver('Word.Application');
fprintf('Word is now opening "%s" ...\n', wordFullFileName);
Word.Documents.Open(wordFullFileName);
fprintf('Document "%s" has been opened in Word. Now making it visible.\n', wordFullFileName);
Word.Visible = true;
Word.Selection.ParagraphFormat.Alignment = 0;
hFig = figure;
for k = 1 : numberOfFiles
pictureFullFileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Reading in %s, and pasting onto document.\n', fileList(k).name);
theImage = imread(pictureFullFileName);
imshow(theImage);
hFig.WindowState = 'maximized';
caption = sprintf('#%d of %d : %s', k, numberOfFiles, fileList(k).name);
title(caption, 'FontSize', 15, 'Interpreter', 'none');
drawnow;
pause(0.6);
copygraphics(gca);
Word.Selection.Paste;
end
close(hFig);
fprintf('Now saving Word document "%s".\n', wordFullFileName);
Word.ActiveDocument.Save();
fprintf('Now shutting down the Word ActiveX server.\n');
Word.Quit;
clear('Word');
promptMessage = sprintf('Done pasting %d images into Word.\nDo you want to open the document in Word?', numberOfFiles);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes - Continue', 'No - Quit', 'Yes - Continue');
if contains(buttonText, 'Yes', 'IgnoreCase', true)
winopen(wordFullFileName);
end
fprintf('Done running %s.m ...\n', mfilename);