Thought this might help some people who are doing similar things:
///////////////////////////////////////////////////////////////////////////////
// Author: Andy Hoyle //
// Date: 18th Sep 2002 //
// Title: Script to create Mailmerge document and perform //
// Mailmerge //
// Version: 1.0 //
// Notes: This script works with Office XP Word on //
// Windows 2000 //
///////////////////////////////////////////////////////////////////////////////
//CONSTANTS USED (found using Object Browser in Microsoft Visual Basic Editor):
$wdFormLetters = 0;
$wdFieldMergeField = 59;
$wdFormatDocument = 0;
$wdSendToNewDocument = 0;
$word = new COM("word.application") or Die ("Did not connect");
$word->Visible = 1;
$doc = $word->Documents->Add();
$doc->Mailmerge->MainDocumentType = $wdFormLetters;
$doc->Fields->Add($word->Selection->Range, $wdFieldMergeField, "Contact_Name");
$doc->Fields->Add($word->Selection->Range, $wdFieldMergeField, "City");
$doc->Fields->Add($word->Selection->Range, $wdFieldMergeField, "Address");
$doc->Fields->Add($word->Selection->Range, $wdFieldMergeField, "Postal_Code");
$doc->Fields->Add($word->Selection->Range, $wdFieldMergeField, "Country");
// Txt document must not have extension .txt or .dat (Word is very particular about what u call the file)
// This is my text document
/
Contact_Name Address City Postal_Code Country
James Oliver 10 King St Accrington EX4 7BG UK
Michaela Williams 10 The Coppice Great Harwood BB2 7QW UK
Sharif Khan 10 Beardswood Ave Blackburn BB1 9PO UK
Robbie Owen 34 Lord Square London LW3 9IY UK
/
$doc->Mailmerge->OpenDataSource("C:\temp\data.info");
$word->ChangeFileOpenDirectory("C:\temp\");
$doc->SaveAs("MergeTemplate.doc", $wdFormatDocument, False, "", False, "", False, False, False, False, False);
$doc->Mailmerge->Destination = $wdSendToNewDocument;
$doc->Mailmerge->SuppressBlankLines = True;
// This tells in not to pause when executing mailmerge
$doc->Mailmerge->Execute(False);
// Save the merged document
$word->ActiveDocument->SaveAs("MergedDocument.doc", $wdFormatDocument, False, "", True, "", False, False, False, False, False);
// This is the bit that gave me the most grief - for some reason, word insists
// on saving the mailmerge template after you perform a merge
// if you dont do this it appears to hang (cause it normally
// display dialog box asking if u want to save before closing)
$doc->Save();
$word->Quit();
unset($word);
echo "\n<br>End of script reached\n";