1

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.

4

1 回答 1

1

好的只是为了关闭我有解决方案......

我不知道为什么,但显然在某些设置上,数据源可能会与文档分离,可能是在重新打开和编辑之后。

我只是添加了一行代码,在数据源保存后和执行合并之前再次打开它。工作一种享受。

谢谢您的帮助

于 2011-08-19T11:01:18.720 回答