How can I edit a Word document without Report Generator?

9 views (last 30 days)
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.

Accepted Answer

Kojiro Saito
Kojiro Saito on 20 Apr 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 Comment
Sara Martos
Sara Martos on 20 Apr 2020
Thank you very much Kojiro!!
Following your instructions, I've already fixed it.
Best regards.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!