How to copy text Font Color and Text Highlight Color from one word to another word document using actxserver

14 views (last 30 days)
We have two versions of same requirement MS word document with some changes and the original document has some text formatting (Font Color and Text Highlight Color). How to apply the same formatting from original version to new version for the text which is matching in two versions. I tried using 'word = actxserver('Word.Application')' to control word from MATLAB, but not able to find exact syntax to achieve the above task. Can anyone please help how to code this? Thanks a lot.

Answers (1)

Praveen
Praveen on 12 Mar 2018
Edited: Praveen on 12 Mar 2018
After little research, i am able to copy 'Font Color' and 'Text Highlight Color' from Original doc to New doc. It is working fine for most of the cases, however few colors it is not reading correctly. Can anyone please kindly let me know how to fix this, so that it works for all colors? Error Example: 1) Even though text in Original document has Purple color, it reading as 'wdPink'. 2) Even though text in Original document has Orange color, it reading as 'wdRed'.
My Code: %Opening Original Document
Word_Original = actxserver('Word.Application'); set(Word_Original,'Visible',1); [fpath,fname,fext] = fileparts('C:\SCRAP\Color_Copy\MNT_Rud_RTS_Test.docx'); out_path = fpath; invoke(Word_Original.Documents, 'Open', [out_path filesep fname fext]);
%Opening New Document Word_New = actxserver('Word.Application'); set(Word_New,'Visible',1); [fpath,fname,fext] = fileparts('C:\SCRAP\Color_Copy\MNT_Rud_RTS_Test_New.docx'); out_path = fpath; invoke(Word_New.Documents, 'Open', [out_path filesep fname fext]);
%Reading the colors from Original document. Select the text and then Text_Color=Word_Original.Selection.Font.ColorIndex; Backgroung_Color=Word_Original.Selection.Range.HighlightColorIndex;
%Applying the colors to text in New document. Select the text and then Word_New.Selection.Font.ColorIndex=Text_Color; Word_New.Selection.Range.HighlightColorIndex=Backgroung_Color;

Categories

Find more on Text Analytics Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!