How can I edit a Word document without Report Generator?

9 visualizaciones (últimos 30 días)
Sara Martos
Sara Martos el 20 de Abr. de 2020
Comentada: Sara Martos el 20 de Abr. de 2020
Hello!
I would like to be able to edit an existing Word document without using Report Generator tool.
I have seen a file that use invoke instruction to open the Word document but I have a error.
Invoke instruction looks for the document in System32 folder but I need it searches in a specific address.
clear all
word = actxserver('Word.Application'); %Start Word
WordFileName='prueba.docx';
hdlWordDoc = invoke(word.Documents,'Open', WordFileName); %Open Document
selection=word.Selection; %set cursor
selection.Pagesetup.LeftMargin=28.34646; %set left Margin to 1cm
selection.TypeText('Hello World!'); %write Text
word.Visible =1;
Error is:
Error using Interface.0002096C_0000_0000_C000_000000000046/Open
Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: We could not find the file. Has it been moved, renamed or deleted?
(C:\WINDOWS\system32\prueba.docx)
Help File: wdmain11.chm
Help Context ID: 604e
Error in Prueba5 (line 5)
hdlWordDoc = invoke(word.Documents,'Open', WordFileName);
Thanks a lot.

Respuesta aceptada

Kojiro Saito
Kojiro Saito el 20 de Abr. de 2020
As you know, the current directory of Word calling from actxserver is C:\WINDOWS\system32, so you need to set a fullpath of the docx file.
word = actxserver('Word.Application'); %Start Word
WordFileName = [pwd filesep 'prueba.docx'];
hdlWordDoc = invoke(word.Documents,'Open', WordFileName); %Open Document
Or, instead of invoke, you can use the following codes.
word = actxserver('Word.Application'); %Start Word
WordFileName = [pwd filesep 'prueba.docx'];
Docs = word.Documents;
Doc = Docs.Open(WordFileName);
  1 comentario
Sara Martos
Sara Martos el 20 de Abr. de 2020
Thank you very much Kojiro!!
Following your instructions, I've already fixed it.
Best regards.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by