1
votes

I'm attempting to make my Delphi membership application automate with word2010, by creating an oleobject instead of a form component:

wrdApp := createoleobject('word.application');

opens a mail merge template:

wrdDoc := wrdApp.Documents.Open('user definable template location')
wrdMailMerge := wrdDoc.MailMerge;

Create and populate a mailmerge called DataDoc.docx complete with fields and data extracted from a membership database.

CreateMailMergeDataDile;
PopulateMailMergeDataFile;

Perform Mail Merge:

wrdMailMerge.Destination := wdSendToNewDocument;
wrdMailMerge.Execute(False);

Clean up:

wrdDoc.Saved := True;
wrdDoc.Close(False);
DeleteFile(DataDoc.docx);

Display Word:

wrdApp.Visible := True;

This works like a charm on my trial version of Word2010, on both single and multiple mailmerges from my membership database.

However I just tested it on a PC with a full Word2010 version and received the error:

"This method or property is not available because the current mail merge main document needs a data source"

The temporary datasource seems correctly populated, but the template document seems unable to use it...

Has any one got any clues to why this is happening, or why there would be a difference between automation with the full and trial version. Perhaps it could even be solved by a plugin?

Many thanks

Edit: Below is my CreateMergeDataFile function:

  procedure TForm1.CreateMailMergeDataFile;
var
  wrdDataDoc : Variant;
begin
// Open a data source from C:\Leisure\Membership\Documents containing the field data
  If FileExists(DocumentDirectory+'\..\DataDoc.docx') then
  DeleteFile(DocumentDirectory+'\..\DataDoc.docx');
  CopyFile(PChar(DocumentDirectory+'\..\MergeFields.docx'),PChar(DocumentDirectory+'\..\DataDoc.docx'),True);
  wrdDoc.MailMerge.OpenDataSource(DocumentDirectory+'\..\DataDoc.docx',EmptyParam, EmptyParam, EmptyParam,
                             EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                             EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                             EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;

Also, this code is only actioned if the msword version is 14.0 (2010) determined by a previously created word.application, thought I was told this wouldn't need to be terminated.

1
There should be no difference in automation. Most probably there is a difference in paths configured in the word instance. Either the template path (though you seem to have that covered) or the path where MS Word is expecting the data source. It looks like it can't find it where it expects it...Marjan Venema
Thanks marjan, it's a relief it's not a different automation. As per my edit above, I use a directory drawn from the programs ini file to ensure it's always correct, and the data source is successfully opened, edited and saved, just not attached to the main document. Very confusing. My full version arrives tomorrow so I can test further.notidaho
If you are using ODBC, is the datasource (either system or user) properly configured on the PC where it isn't working?Marjan Venema
Sorry, I'm not too familiar with Delphi yet. What do you mean by properly configuring the (system or user) datasource on a PC? and how would I go about doing so.notidaho
Has nothing to do with Delphi, but with ODBC (Open Database Connectivity): support.microsoft.com/kb/110093 . You can open an MS Access database or Excel sheet from Delphi using an ODBC connection for the dataset, but you have to configure the connection using the ODBC control panel app (or something similar). If you don't know what ODBC is, you are probably not using it...Marjan Venema

1 Answers

1
votes

OK just for closure I have the solution...

I don't know why but apparently on some Setups the datasource can become detached from the document, maybe after it's re-opened and edited.

I simply added a line of code to open the datasource again after it's saved and before the merge is executed. Works a treat.

Thanks for the help