How can I edit a Word document without Report Generator?

6 ビュー (過去 30 日間)
Sara Martos
Sara Martos 2020 年 4 月 20 日
コメント済み: Sara Martos 2020 年 4 月 20 日
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.

採用された回答

Kojiro Saito
Kojiro Saito 2020 年 4 月 20 日
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 件のコメント
Sara Martos
Sara Martos 2020 年 4 月 20 日
Thank you very much Kojiro!!
Following your instructions, I've already fixed it.
Best regards.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by