필터 지우기
필터 지우기

Changing the outputted font of a wordApp code

조회 수: 5 (최근 30일)
Michael
Michael 2024년 4월 24일
답변: Brahmadev 2024년 4월 24일
Hi, I've begun writing code to open a microsoft word template, generate some outputted times and numbers, and fill the document with this info. Currently the defualt output is Aptos. How can this be changed?
My code:
% Define the file path of the existing Word document
docName = 'test_template1.docx'; % Your document filename
docPath = 'C:\Users\micha\OneDrive\Desktop\Tickets\test_template1.docx';
% Check if the file exists
if exist(docPath, 'file')
% Create a Word application
wordApp = actxserver('Word.Application');
% Check if Word application is activated
if ~isempty(wordApp)
try
% Open the existing document
wordDoc = wordApp.Documents.Open(docPath);
% FILL 1 - ID NUMBER
rng(0,'twister');
idNum = randi([740 9850],1,1);
idNum2 =('idNum');
% DATA FILL 2 - AREA
area = ('28213');
% DATA FILL 3 1st date
currentDate = upper(datestr(now, 'ddd dd mmm '));
% Variable Set time (currently 6:30 PM)
setTime = ('06:30');
% Add text to the document
wordApp.Selection.EndKey(6); % Go to the end of the document
wordApp.Selection.TypeParagraph; % Insert a new paragraph
wordApp.Selection.TypeText([idNum2, area, currentDate , setTime]);
% Save the document
wordDoc.Save;
% Close the document
wordDoc.Close;
% Quit Word application
wordApp.Quit;
disp('Document updated successfully.');
catch exception
disp('An error occurred:');
disp(exception.message);
% Close Word application
wordApp.Quit;
end
else
disp('Word application could not be activated.');
end
else
disp('The specified Word document does not exist.');
end
Thanks

답변 (1개)

Brahmadev
Brahmadev 2024년 4월 24일
If you would like to change the format of the data that is being displayed in the Word file. You can format you code as shown below:
% Define the file path of the existing Word document
docName = 'test_template1.docx'; % Your document filename
docPath = 'C:\Users\micha\OneDrive\Desktop\Tickets\test_template1.docx';
% Check if the file exists
if exist(docPath, 'file')
% Create a Word application
wordApp = actxserver('Word.Application');
% Check if Word application is activated
if ~isempty(wordApp)
try
% Open the existing document
wordDoc = wordApp.Documents.Open(docPath);
% FILL 1 - ID NUMBER
idNum = randi([740, 9850], 1, 1);
% Convert idNum to string if you want to concatenate it with other strings
idNumStr = num2str(idNum);
% DATA FILL 2 - AREA
area = '28213';
% DATA FILL 3 - 1st date
currentDate = upper(datestr(now, 'ddd dd mmm '));
% Variable Set time (currently 6:30 PM)
setTime = '06:30';
% Format the output string (example format)
outputText = sprintf('ID Number: %s, Area: %s, Date: %s, Time: %s', idNumStr, area, currentDate, setTime);
% Add formatted text to the document
wordApp.Selection.EndKey(6); % Go to the end of the document
wordApp.Selection.TypeParagraph; % Insert a new paragraph
wordApp.Selection.TypeText(outputText);
% Save the document
wordDoc.Save;
% Close the document
wordDoc.Close;
% Quit Word application
wordApp.Quit;
disp('Document updated successfully.');
catch exception
disp('An error occurred:');
disp(exception.message);
% Close Word application
wordApp.Quit;
end
else
disp('Word application could not be activated.');
end
else
disp('The specified Word document does not exist.');
end
Hope this helps in resolving your query!

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by