Delphi MailMerge Word2010新建页面命令

发布于 2024-11-18 23:57:23 字数 564 浏览 4 评论 0 原文

我正在尝试更新用 Delphi 编写的会员软件,以将邮件合并到 Word2010 中。

然后,对于模板中的每个合并字段名称,我将其替换

wrdApp := CreateOleObject('Word.Application');
wrdDoc := wrdApp.Documents.add();

为表中相应的字段值。

由于之前的回答,我已成功为单个成员完成此操作: Delphi 5 中的 MS Word 2010 mailmerge

但是,我在为表中的所有成员迭代此代码时遇到问题。理想情况下,我需要模板显示在同一文档的新页面上,并显示下一个成员的详细信息。

目前我只能为每个成员覆盖相同的文档,或者每次创建一个新文档。

任何人都可以建议我如何在下一页上重新创建模板、将多个文档合并到同一个文档中或建议一种替代方法。

非常感谢

I am trying to update my membership software written in Delphi, to mailmerge in to 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦晓ヶ微光ヅ倾城 2024-11-25 23:57:23

您实际上并没有创建邮件合并。

这是一些可能有帮助的代码(请参阅下面的注释)。它使用 Delphi 的 TWordApplication 组件来处理连接并提供数据类型,但它应该为您提供进入的方向:

// 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;

注意:此代码的大部分是从下载的 示例 我从 Deborah Pate 的 页面。 freeserve.co.uk/" rel="nofollow">网站。 (Deborah 曾经(并且可能仍然是)TeamB 的成员。)我只是将其修改为通过 TWordApplication 连接,并将其更新为在 Delphi XE 下编译。

正如我所说,它使用 TWordApplication 建立与 Word 的连接并公开所使用的方法和类型。其他一切都在代码本身中完成。

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:

// 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 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.

As I said, it's using TWordApplication to make the connection to Word and expose the methods and types used. Everything else is done in the code itself.

天荒地未老 2024-11-25 23:57:23

看起来您并没有真正执行邮件合并,而是在搜索和替换之前执行。这是有原因的吗?否则为什么不建立一个“真正的”邮件合并来解决您的问题呢?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文