How to save a .docx Word document on a specific path?

14 次查看(过去 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 %%%

采纳的回答

Walter Roberson
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
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
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 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by