Forum


Replies: 1   Views: 2566
Replacevariablebywordfragment replaces the whole header instead of just variable.
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 lalit  · 14-08-2018 - 12:49

I am trying to replace a variable $PageNo$ in header of the templte .docx file. However it replace the whole header with the value of variable.

Below is the script I am using.

Could some body tell what's wrong with it?

 

<?php

 

require_once '../../../classes/CreateDocx.php';

 

$docx = new CreateDocxFromTemplate('example_addFootnote_2.docx');

 

// create the Word fragment that is going to replace the variable

$wf = new WordFragment($docx);




 

// insert all the content in the Word fragment we are going to use for replacement

$wf->addText("hello I replaced the variable.");

 

$docx->replaceVariableByWordFragment(array('PageNo' => $wf), array( 'target' => "header"));

 

$docx->createDocx('example_replaceVariableByWordFragment_1');

print "OK";

 

?>

 

 

Header in template .docx file 

Hello I am default header. 2mdc.com with random image:
Page Number:   $PageNo$

 

 

 

Posted by admin  · 14-08-2018 - 13:54

Hello,

You are doing a block type replacement that removes the whole paragraph of the placeholder. You need to use an inline type replacement.

Regards.