Merge or insert different Word files into a final Word file using Actxserver

7 views (last 30 days)
Hello,
I would like to know it somebody can help with this. I´m not sure if it is possible to do it. Lets say that I have several Word files with different information in them (documents A, B and C). The objective is to use Matlab, to create a unique and final Word document whose different sections are the whole content of the documents A, B and C. Is there any way that using Matlab with actxserver to create the final document, by copying and pasting information read from documents A, B and C in a easy way?
wordaplication = actxserver('Word.Application');
final_doc=invoke(wordaplication.Documents,'Add','D:\Test_Report.docx');
docA=invoke(wordaplication.Documents,'Add','D:\Test_ReportA.docx');
docB=invoke(wordaplication.Documents,'Add','D:\Test_ReportB.docx');
docC=invoke(wordaplication.Documents,'Add','D:\Test_ReportC.docx');
then.something like this
for each Doc
content_doc=read (Doc)
paste content_doc in final_doc
end
Thanks in advance!
  1 Comment
M Angeles de la Cruz
M Angeles de la Cruz on 20 Mar 2020
I have been investigating and I would like to use the option from MS Word of inserting a Word file in order to perform the merge. To learn how to use it, I have recorded a macro performing this activity on Word, and the resulting code is the following:
Selection.InlineShapes.AddOLEObject( ClassType:="Word.Document.12", FileName:="C:\Users\mangeles.delacruz\Desktop\Test\Doc1.docx", LinkToFile :=False, DisplayAsIcon:=False)
I´m trying to call this function from matlab but I get errors all the time... I´m not very familiar with actxserver with Word... This is the code I´m writing:
wordaplication = actxserver('Word.Application');
wordaplication.Visible = 1; %Word visible or not, put 0 if it is not desired to see the docx editing
doc=invoke(wordaplication.Documents,'Add','D:\Test_Report.docx');
selection = wordaplication.Selection;
selection.InlineShapes.AddOLEObject('C:\Users\mangeles.delacruz\Desktop\SW RFSCOE\hola.docx',0,0);
Then I get a message on the word document saying "word cannot obtain data from link 0". I clic on accept and then I also get the following error on Matlab console:
Error using Interface.Microsoft_Word_14.0_Object_Library.InlineShapes/AddOLEObject
Error in pruebasWords (line 15)
selection.InlineShapes.AddOLEObject('C:\Users\mangeles.delacruz\Desktop\SW RFSCOE\hola.docx',0,0);
I think this function is the key to solve my need... but I´m not using it right.
Anybody with more experience can help?
Thank you!!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!