1
votes

I am trying to update my membership software written in Delphi, to mailmerge in to Word2010.

I'm using

wrdApp := CreateOleObject('Word.Application');
wrdDoc := wrdApp.Documents.add();

then for each merge field name in a template I'm replacing it with the corresponding field value from a table.

I have successfully done this for single members thanks to a previous answer: MS Word 2010 mailmerge in Delphi 5

However I'm having trouble iterating this code for all members in the table. Ideally I need the template to appear on a new page in the same document with the next members details on.

Currently I can only overwrite the same document for each member, or create a new document each time.

Can anyone advise me how to either recreate the template on the next page, merge multiple documents in to the same one or suggest an alternative method.

many thanks

2
Windows.Application should probably be Word.Application.Andreas Rejbrand
yea typo on OP sorry, question still valid. Cannot infact open multiple documents as it seems the same template .doc can't be opened multiple times. I thought about pasting the content of the template multiple times in to the same new document but it would be hard to populate each iteration with different details. Any ideas?notidaho

2 Answers

1
votes

You're not actually creating a mailmerge.

Here's some code (see note below) that might help. It's using Delphi's TWordApplication component to handle the connection and provide the datatypes, but it should give you a direction to go in:

// Drop a TWordApplication (from the Servers palette page) on a new blank form,
// and change it's name to WordApp. No other property changes or components are
// used.

procedure TForm1.FormCreate(Sender: TObject);
var
  Doc: _Document;
  DBName: string;
  Pause, SQL, Connection, SaveChanges: OleVariant;
begin
  WordApp.Connect;
  Doc := WordApp.Documents.AddOld(EmptyParam, EmptyParam);
  WordApp.Visible := True;

  SetUpMergeDoc(Doc);
  DBName := 'YourDatabasePath\DatabaseName';
  Connection := 'YourADOorODBCConnectionString';
  SQL := 'SELECT * FROM customer';
  Doc.MailMerge.OpenDataSource(DBName, EmptyParam, EmptyParam, EmptyParam,
                               EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                               EmptyParam, EmptyParam, EmptyParam, Connection,
                               SQL, EmptyParam, EmptyParam, EmptyParam);
  // Do the actual mailmerge.
  Pause := False;
  Doc.MailMerge.Destination := wdSendToNewDocument;
  Doc.MailMerge.Datasource.FirstRecord := wdDefaultFirstRecord;
  Doc.MailMerge.Datasource.LastRecord := integer(wdDefaultLastRecord);
  Doc.MailMerge.Execute(Pause);

  // Save the mailmerged document
  SaveChanges := wdDoNotSaveChanges;
  WordApp.Quit(SaveChanges, EmptyParam, EmptyParam);
  Doc := nil;
  WordApp := nil;
end;

procedure TForm1.SetUpMergeDoc(Doc: _Document);
var
  R: Range;
  Direction: OleVariant;
begin
  R := Doc.Range(EmptyParam, EmptyParam);

  Direction := wdCollapseEnd;
  R.InsertAfter('Dear ');
  R.Collapse(Direction);

  { Insert a field with the name of the datasource field }
  Doc.MailMerge.Fields.Add(R, 'Company');
  R := Doc.Range(EmptyParam, EmptyParam);
  R.Collapse(Direction);
  R.InsertParagraphAfter;
  R.InsertAfter('We have yet to receive payment for our invoice of ');
  R.Collapse(Direction);
  Doc.MailMerge.Fields.Add(R, 'LastInvoiceDate');
  R := Doc.Range(EmptyParam, EmptyParam);
  R.Collapse(Direction);
  R.InsertAfter('.');
  R.InsertParagraphAfter;
  R.InsertAfter('Cough up or we''ll send the boys round.');
  R.InsertParagraphAfter;
end;

NOTE: Much of this code was obtained from a downloaded example I got from the Word automation page of Deborah Pate's website. (Deborah was (and may still be) a member of TeamB.) I simply modified it to connect via TWordApplication and updated it to compile under Delphi XE.

As I said, it's using TWordApplication to make the connection to Word and expose the methods and types used. Everything else is done in the code itself.

0
votes

It seems you are not really performing a mailmerge but fore of a search and replace. Is there a reason for this? Otherwise why not just set up a 'real' mailmerge which would solve your problems.