开发者

Mailmerge in MSOffice 2010 vs Trial version, with Delphi

开发者 https://www.devze.com 2023-03-28 21:30 出处:网络
I\'m attempting to make my Delphi membership application automate with word2010, by creating an oleobject instead of a form component:

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;
PopulateMai开发者_StackOverflow中文版lMergeDataFile;

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消