14
Jan
Written by: Jonathan Maron. Stored in: Templates.

A core part of any phpLiveDocx project is the creation of a template file. Using Microsoft Word 2003, this takes only a matter of minutes:

  1. Open Microsoft Word 2003 to create a new document.
  2. Select Insert, Field… from the main menu to open the Field dialog box.
  3. Choose the MergeField from the Mail Merge category and specify a name for the new field: name.
  4. Specify the format and some field options as shown in the following screenshot:

    Template in Microsoft Word

  5. Repeat step 2 and 3 to insert another field and name this field “company”. Additionally, you could change the text of both fields. The finished template should now look like this:

    Merge field in Microsoft Word

Consider the following PHP5 code, which can be used to populate the template created above. Assume that the template has been saved with the filename template.docx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$mailMerge = new Zend_Service_LiveDocx_MailMerge();
 
$mailMerge->setUsername('myUsername')
          ->setPassword('myPassword');
 
$mailMerge->setLocalTemplate('template.docx');
 
$mailMerge->assign('NAME',    'Mitchel');
$mailMerge->assign('company', 'Flowers Direct 2000');
 
$mailMerge->createDocument();
 
$document = $mailMerge->retrieveDocument('pdf');
 
file_put_contents('document.pdf', $document);

For a discussion on the logic of this source code, please see this post.

In future blog posts, we will be looking at how to create templates with Open Office and TX Template Designer.

2 Responses to “Template creation in Microsoft Word 2003”

  1. Simon Says:

    The MergeFields thing is clear. Thanks for that.
    But I still didn’t find out how to create a MergeBlock in Word (both 2003 and 2007)

    BTW: What about a short tut for OpenOffice.org?

  2. Björn Meyer Says:

    Hello Simon

    The merge block format is described in the TX Text Control documentation:

    http://www.textcontrol.com/support/documentation/html/dotnet/n_aspnet_mailmerge.repeating_blocks.htm

    A block simply consists of 2 bookmarks with a specific name. The text including fields between those bookmarks will be repeated.

Leave a Reply