preload
11 Comments | Jun 21, 2011

Merging FileMaker Data Directly Into MS Word

Estimated Time To Read This: 3 – 5 minutes      


Have you ever wanted to get data from your FileMaker database directly into an MS Word document?  Doing so isn’t all that difficult (though it will involve a bit of programming).  If you follow this example, you won’t need any third party plugins, you’ll get to build your template file within MS Word, and you don’t even have to go through any confusing merge fields wizards in MS Word either.  The Write To Word – Example Files that I’m providing will work only on the Windows platform, although similar steps should be possible on the OSX platform using AppleScript.

Basically this process entails a glorified search and replace that’s being initiated from within a FileMaker Pro script.  You start off using a .dotx file (A MS Word Document Template).  My example template looks like this:

Before

After you’ve run the FileMaker Pro script, it will search and replace the appropriate text, so you end up with something that looks like this:

After

I took advantage of a very old technology known as DDE to get this to work.  FileMaker (on Windows) has a Script Step called “Send DDE Execute” which takes a few parameters:

  1. Service Name –> here you type in the name of the DDE Server –> in this case “winword”
  2. Topic –> this is typically the name of the file that you’re going to interact with –> in this case we’ll use “system” which will basically tell WinWord to use whichever file is currently open (normally not a good idea, but you’ll see in the next step that we’re going to be opening a new document anyway)
  3. Commands –> this is where the meat and potatoes of the routine are to be found.  Here we tell WinWord that we want to use the .dotx template file in order to create a new document within Word, and once that’s been created, we do our search and replace.  Details of this can be found in the codebox below
"[FileNew.Template =\"" & $templatePath & "\"]" &
"[EditReplace.Find=\"<<firstName>>\",.Replace=\"" & WriteToWord::nameFirst & "\",.ReplaceAll,.Wrap=1]" &
"[EditReplace.Find=\"<<lastName>>\",.Replace=\"" & WriteToWord::nameLast & "\",.ReplaceAll,.Wrap=1]"

Once all the pieces are intact, the process of getting this to work is very simple, but there are a few steps you’ll need to follow.  Here’s how you can get my Write To Word – Example Files working for you:

  1. Save my “FileMaker.dotx” file to your “My Documents” folder
  2. Open MS Word (no need to have any Word Documents open though)
  3. Open my “WriteToWord.fp7″ file
  4. Specify the “First Name” and “Last Name” that you desire to appear within your new Word Document
  5. Click the “Write To File” button

You should now have a Word document that looks much like the “After” image depicted above.  Have you got any novel uses for the “Send DDE Execute” step?  If so, I’d love to hear how you’re using it.  I think it’s going to be a big time saver for me, and might radically change the way that I design parts of my systems.


Tags:, ,





Related Articles


11 Comments

GailK 1:27 pm - 23rd June:

Was excited until I saw it was Windoz only :-/

Matthew Leering 4:11 pm - 27th June:

Hey Gail!
Wim’s suggestion from the comments below point out a way that the same type of functionality can be accomplished using VBScript. In many cases, the same types of things that you can accomplish using VBScript can also be accomplished using AppleScript. Might be an avenue worthwhile looking into. :)

GailK 4:13 pm - 27th June:

Thanks! I’ll give it a try.

Wim Decorte 6:16 am - 24th June:

DDE is very much dead technology and only still there for backward compatibility. A similar but ultimately more stable approach can be done with VBscript and the Send Event script step:
- create a VBscript that uses the Word object model, store it in a text field, with placeholdes where some of the variable code goes
- use substitute to create the final VBscript
- use Export field contents to produce the VBscript on the hard disk
- use Send Event to execute it.
Now here’s the cool benefit: unlike DDE code, you can do error trapping and handling in your VBscript code and from inside the VBscript you can talk back to FM using the FM ActiveX features

Matthew Leering 9:12 am - 24th June:

I’m going to have to give this a try. Thanks Wim!

Peter Bee 4:56 am - 25th November:

Works great !

If there is more than one filemaker record, it creates a new document for each record.

Is there a way to create one document with a new page for each additional record?

Matthew Leering 2:26 pm - 25th November:

Hi Peter;
Glad to hear you found it helpful.
:)

While I’m sure that there would be a way to accomplish what you’re looking to do, this technique is definitely not suited for the task, as it’s basically a search and replace technique. I’m not sure what the best approach would be for your request, though your OS might be able to help you out if it’s okay that your final product be in PDF format. If your computer is already set up to be able to print to PDF files then you can simply select multiple files from your shell (Explorer on Windows/Finder on MAC). Usually when you right-click on them, it should give you the option to print the files. Pick that option, but instead of printing to a physical printer, choose to print to a PDF. This should allow you to be able to create a single PDF document from multiple documents.

Oh, and if you don’t have a PDF printer driver installed, there are usually free ones to download.

Peter Bee 7:08 am - 29th November:

Hi Matthew
We are merging data into a MS Word file for and emailing to our clients. One page for each FM record. Clients make changes into the Word document, and send back to us.
With PDF’s this is not possible if clients do not have the required Acrobat software.
Merging into Word from Filemaker used to work well, until we moved to Word 2007. Now Word seems to lose the source (merge data) file quite often, so I am looking for another solution.
Your’s looked perfect, pity it cannot handle mutiple records.
Have to look at the merge function again, probably just missing something simple…
Regards
Peter

Wim Decorte 12:04 pm - 1st December:

Hi Peter,

One way around that is to use VBscript to do the talking to Word. Instead of “embedding” the data source reference in the Word document, you can have VBscript set it on the fly. You can store the VBScript code in a FM fiield, do an export field contents and a send event to fire it off.

Andy Sheppard 5:59 pm - 31st December:

Win,

Could you explain this more? Maybe some sample code or a walk-through? Sorry for being a noob – just trying to figure it all out.

Wim Decorte 10:50 am - 11th January:

Hi Andy, a full reply would detract from the purpose of this article. But head to http://www.fmforums.com and look for the Windows Automation forum there. If you don’t find an answer there, post the question.

Leave a Reply

* Required
** Your Email is never shared