Recent Articles
Article #139

Building Word documents from your FileMaker database.

You can use plug-ins to make simple modifications to Word documents, but if you need something more flexible, use these techniques.

Anatomy of a .docx file

Word documents, or files with the .docx extension, are actually a zip file comprised of xml documents and other resources. Take a .docx file, change the extension to .zip, and then unzip it. You will see something like this:

You can reverse the process: Zip the files and change the extension back to .docx and you get the original Word file. So, to build your own Word documents you just need to build a set of files and zip them up.

Instead of starting from scratch, create a document from within the Word application that will serve as a template. When building the file to include FileMaker data, identify the places in the XML that will be replaced with your data. In many cases, all you need to do is replace content within word/document.xml.

It is helpful to be familiar with the XML grammaer used in these files. Wikipedia has some general information:

Office Open XML

The best way to learn how the XML is put together is to examine samples from actual Word documents. Once you understand the XML, then you can build anything you want, including styling, tables, and documents with graphics from FileMaker container fields.

Escaping text

You'll want to escape any text that you output to XML files, perhaps in a custom function, like this:

					Substitute ( text ; 
					
					["&" ; "&"] ; 
					["<" ; "&lt;"] ; 
					["¶¶" ; "¶"] ; 
					[">" ; "&gt;"] ; 
					["\"" ; "&quot;"] ; 
					["\“" ; "&quot;"] ; 
					["‘" ; "'"] ; 
					["’" ; "&apos;"] ; 
					[Char ( "9" ) ; "  "] ; 
					[Char ( "8211" ) ; "-"] ; 
					["•" ; ""] ; 
					["-" ; "-"] ; 
					["\”" ; """] ; 
					["\”" ; """] ; 
					//["®" ; "®"] 
					["®" ; "&#174;" ] ; 
					[ "¾" ; "&#190;" ] ; 
					["—" ; "&#8212;" ] 
					
					)
				

Zipping files

It's simple to build XML in a text field and export it, but FileMaker can't zip files, so you'll need a plug-in like Troi file, or you can use PHP with the Smartpill plug-in.

Using the Smartpill plug-in, you can use PHP inside a custom function to zip files, like this:

//_php_zip ( sourcePath ; targetPath )


Let ( [ code = "$result = Zip(" & Quote ( sourcePath ) & "," & Quote ( targetPath ) & "); echo $result; function Zip($source, $destination) {$test = ''; if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ZipArchive(); if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { return false; } $source = str_replace('\\\', '/', realpath($source)); if (is_dir($source) === true) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $test += $file;$file = str_replace('\\\', '/', $file); if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) continue; $file = realpath($file); if (is_dir($file) === true) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else if (is_file($source) === true) { $zip->addFromString(basename($source), file_get_contents($source)); } return $zip->close(); } " ; $$code = code ] ; PHP_Execute ( code ) )

Don't forget to leverage styles because they simplify formatting and make building the XML easier than when using in-line formatting.

Have fun!

- - - - - - - - -

Brent Durland
Swingtree Consulting

FileMaker Forever is a resource for FileMaker enthusiasts. Come here often for tips and sample files to help with your own projects. Created by Brent Durland with Swingtree Consulting