简体   繁体   English

Delphi MailMerge Word2010新页面命令

[英]Delphi MailMerge Word2010 new page command

I am trying to update my membership software written in Delphi, to mailmerge in to Word2010.我正在尝试更新用 Delphi 编写的会员软件,以邮件合并到 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由于之前的回答,我已经为单身成员成功完成了这项工作: 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非常感谢

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:它使用 Delphi 的TWordApplication组件来处理连接并提供数据类型,但它应该为您提供指向 go 的方向:

// 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 Pate网站Word 自动化页面下载的示例中获得的。 (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. (Deborah 是(并且可能仍然是)TeamB 的成员。)我只是将它修改为通过TWordApplication进行连接,并将其更新为在 Delphi XE 下编译。

As I said, it's using TWordApplication to make the connection to Word and expose the methods and types used.正如我所说,它使用TWordApplication与 Word 建立连接并公开使用的方法和类型。 Everything else is done in the code itself.其他一切都在代码本身中完成。

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.否则为什么不设置一个“真正的”邮件合并来解决你的问题。

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

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