Borrar filtros
Borrar filtros

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

14 visualizaciones (últimos 30 días)
IKER ARRIEN
IKER ARRIEN el 1 de Nov. de 2017
Comentada: Jorge Pascual el 17 de Jun. de 2020
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 %%%

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Nov. de 2017
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 comentarios
Walter Roberson
Walter Roberson el 16 de Jun. de 2020
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 el 17 de Jun. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Report Generator en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by