Forum


Replies: 3   Views: 1260
Using replacevariablebywordfragment() replaces the intial format from the word document
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by philip0908  · 18-03-2021 - 14:50

Hello, 

I have a Word document with a couple of placeholders that I want to replace. Some of the placeholders are formatted, e.g. in bold, italic, and so on.

However, I wanted the replacements to be highlighted in yellow, so I can clearly see, where I need to doublecheck.

When I got a formatted document like this:

     This is a text with a $Variable$.

I want the outcome to be like this:

     This is a text with a replaced Text. (Also highlighted in yellow but this editor cannot do this).

However, the texts comes like this:

   This is a text with a replaced Text. (Also highlighted in yellow but this editor cannot do this).

 

My code:

...

$docx = new CreateDocxFromTemplate($document);

$word_fragment = new WordFragment($docx);

$word_fragment->addText("replaced Text", array("highlightColor" => "yellow")); 

$docx->replaceVariableByWordFragment(array("Variable" => $word_fragment), $options);

 

Any ideas/help?