1
Vote

setValue() not working

description

here is my php code:
$PHPWord = new PHPWord();
$template = $PHPWord->loadTemplate(APPPATH.'files/test.docx');
$template->setValue('test1', 'hello');
$template->setValue('test2', 'whats');
$template->setValue('test3', 'up');
$template->setValue('test4', 'koby');
$template->setValue('test5', '!!!!!!');
$template->save('files/test.docx');
here is my word document text:

${test1}
${test2}
${test3}
${test4}
${test5}

The output word content:

hello
${test2}
${test3}
${test4}
${test5}

setValue(); only sets the value for the first line ${test1}

Any help would be great

comments

aburgett wrote Jul 3, 2013 at 8:18 PM

The issue is with your template and not with the PHP code. If you open the template with a zip program and then view the document.xml file in the 'word' folder, you'll see the basic XML of the document. All of your template declarations are split in pieces, except #1. This is something I've run into with Word for years. You can safely delete everything between the pieces of your template variables. Here is an example of what I mean, with ${test5} being broken apart:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
    <w:body>
        <w:p w:rsidR="00AC49DC" w:rsidRDefault="00243DD6">
            <w:r>
                <w:t>${test1}</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00243DD6" w:rsidRDefault="00243DD6">
            <w:r>
                <w:t>${test2}</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00243DD6" w:rsidRDefault="00243DD6">
            <w:r>
                <w:t>${test3}</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00243DD6" w:rsidRDefault="00243DD6">
            <w:r>
                <w:t>${test4}</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00243DD6" w:rsidRDefault="00243DD6">
            <w:r>
                <w:t>${</w:t>
            </w:r>
            <w:bookmarkStart w:id="0" w:name="_GoBack"/>
            <w:bookmarkEnd w:id="0"/>
            <w:r>
                <w:t>test5}</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00243DD6" w:rsidSect="00C107D0">
            <w:pgSz w:w="12240" w:h="15840" w:code="1"/>
            <w:pgMar w:top="907" w:right="907" w:bottom="907" w:left="907" w:header="720" w:footer="720" w:gutter="0"/>
            <w:cols w:space="720"/>
            <w:docGrid w:linePitch="272"/>
        </w:sectPr>
    </w:body>
</w:document>
Adam

aburgett wrote Jan 9, 2014 at 3:14 AM

Please vote if this was helpful or solved your problem.

olev wrote Oct 22, 2015 at 12:41 AM

ok but what is the solution for this, how do i edit the function setValue