How to save a .docx Word document on a specific path?
조회 수: 12 (최근 30일)
이전 댓글 표시
I have created a GUI where the last step is to create a Word report with the results, using "import mlreportgen.dom.*;"
I ask the user to specify the path where he wants to save the Word via "uiputfile".
Therefore, I have obtained the full name (path + name) of the Word report.
Then I use the function "save" with the full name of the report (name + path).
After that I would like to automatically open that Word report.
However, using the function "save", the Word that is saved in that specific path is corrupted, so it does not open correctly.
I have seen on the Help documentation that "save" does not apply for ".docx" files. So, is there any other way to save the Word report on that specific path set by the user via uiputfile?
Below I attach part of the code I am using, in case it helps:
%%%MATLAB CODE %%%
Report_full_name = strcat(Report_name,'.docx'); %From 'Report' to 'Report.docx'
[Saved_Name,Saved_path] = uiputfile(Report_full_name,'Save file name'); %Obtain path: C\Folder (etc)
Saved_Name_and_Path = strcat(Saved_path,Saved_Name); %Create full name: C\Folder\Report.docx
save(Saved_Name_and_Path); %Save the file C\Folder\Report.docx
open(Saved_Name_and_Path); %Open the file C\Folder\Report.docx
%%%END %%%
댓글 수: 0
채택된 답변
Walter Roberson
2017년 11월 1일
It looks to me as if you should be using Document() to create a document container and specify the path, and open() that document to prepare it for writing, then do a series of steps to add the content, and then close() the document object to generate the report and save it.
댓글 수: 4
Walter Roberson
2020년 6월 16일
Use fullfile(Path,Name) instead of strcat(Path,Name) as the directory name returned by uiputfile() does not always have a directory seperator .
Jorge Pascual
2020년 6월 17일
I still having the same problem. It allows me to save the file, but then I have an error when I try to open it.
word = actxserver('Word.Application'); %start Word
word.Visible =1; %make Word Visible
document=word.Documents.Add; %create new Document
selection=word.Selection; %set Cursor
selection.Font.Name='Times New Roman'; %set Font
selection.Font.Size=11; %set Size
selection.ParagraphFormat.Alignment =3;
selection.Paragraphs.LineUnitAfter=0.5; %sets the amount of spacing
%between paragraphs(in gridlines)
selection.TypeText(Texto); %write Text
selection.TypeParagraph; %line break
[Saved_Name,Saved_path] = uiputfile(Nombre_Fich,'Save file name')
Saved_Name_and_Path = fullfile(Saved_path,Saved_Name);
save(Saved_Name_and_Path); %Save the file C\Folder\Report.docx
%
% document.saveas([Nombre_Fich, selpath]); %save Document
word.Quit();
'Texto' is the text that I want to write and save in the .docx file.
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!