简体   繁体   English

MSOffice 2010 与试用版中的邮件合并,带有 Delphi

[英]Mailmerge in MSOffice 2010 vs Trial version, with Delphi

I'm attempting to make my Delphi membership application automate with word2010, by creating an oleobject instead of a form component:我正在尝试通过创建 oleobject 而不是表单组件来使我的 Delphi 会员应用程序使用 word2010 自动化:

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.创建并填充名为 DataDoc.docx 的邮件合并,其中包含从成员数据库中提取的字段和数据。

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.这就像我的 Word2010 试用版的魅力一样,适用于我的会员数据库中的单个和多个邮件合并。

However I just tested it on a PC with a full Word2010 version and received the error:但是,我刚刚在具有完整 Word2010 版本的 PC 上对其进行了测试,并收到错误消息:

"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:编辑:下面是我的 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.此外,仅当 msword 版本为 14.0 (2010) 时才会执行此代码,该版本由先前创建的 word.application 确定,我以为我被告知这不需要终止。

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谢谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM