邮件合并 IF 字段和 VBA 出现问题

发布于 2024-11-09 23:53:09 字数 550 浏览 3 评论 0原文

我有一个非常简单的邮件合并,它连接到 SQL Server 数据库

邮件合并有两个字段,一个是

{MERGEFIELD Dealer_Name}

另一个是下面的 IF 字段

{IF {MERGEFIELD Dealer_Name}="Joe" "1" "0"}

但是 IF 字段不执行。 Dealer_Name 合并字段确实执行并显示每个经销商名称或每个页面,但 IF 字段仅显示为 Dealer_Name}=

也可以编写 VBA 函数或过程,然后将其添加到单词邮件中将文档合并为字段或合并按钮或其他内容。

例如,假设我有以下 VBA 过程,

Public Sub PrintSomeText  
  Selection.TypeText("Hello World")
End Sub

我可以将其作为宏或其他内容添加到邮件合并文档中,以便它在放置到文档中的位置的每个页面上打印出“Hello World”吗?

I have a very simple mail merge that is connected to an SQL server database

The mail merge has two fields on it, one is

{MERGEFIELD Dealer_Name}

the other is the following IF field

{IF {MERGEFIELD Dealer_Name}="Joe" "1" "0"}

However the IF field does not execute. The Dealer_Name merge field does execute and displays each dealer name or each page but the IF field just displays as Dealer_Name}=

Also is it possible to write a VBA function or procedure and then add it to the word mail merge document as a field or merge button or something else.

For example let say I have the following VBA procedure

Public Sub PrintSomeText  
  Selection.TypeText("Hello World")
End Sub

Could I add this to the mail merge document as a macro or something so that it prints out "Hello World" on each page in the locate that it was placed onto the document?

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

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

发布评论

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

评论(1

一场信仰旅途 2024-11-16 23:53:09

我能够让 IF 字段代码工作,不知道如何工作,它刚刚开始工作。

我还发现您可以通过使用 DOCVARIABLE 或 REF 字段代码在 Word 邮件合并文档中使用 VBA 代码。

对于遇到此问题的其他人:

添加如下所示的文档变量:

{DOCVARIABLE MyVariable}

或如下所示的书签引用:

{REF MyBookmarkReference}

然后在 VBA (Alt+F11) 中执行以下操作:

在项目中(项目名称),例如 Project(MailMergeDemo)
在“Microsoft Word 对象”下
双击“ThisDocument”

并输入以下代码

Dim WithEvents app As Application

Private Sub Document_Close()
  Set app = Nothing
End Sub

Private Sub Document_Open()
  Set app = Application
End Sub

从下拉列表中选择应用程序对象,然后选择 app_MailMergeBeforeRecordMerge 事件并输入以下代码:

Private Sub app_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
  Doc.Variables("MyVariable").Value = Doc.MailMerge.DataSource.DataFields(SomeFieldName).Value

  Doc.Bookmarks("MyBookmarkReference").Range.Text = "this is a test"

  Doc.Fields.Update
End Sub

您将需要关闭并重新打开文档以挂钩应用程序对象。

请参阅 http://support.microsoft.com/kb/285333 了解更多信息。

I was able to get the IF field code working, not sure how, it just started working.

Also I found that you can use VBA code in Word mail merge documents by using the DOCVARIABLE or REF field codes.

For anyone else who has this problem:

Add a document variable like this:

{DOCVARIABLE MyVariable}

or a bookmark reference like this:

{REF MyBookmarkReference}

Then in VBA (Alt+F11) do the following:

In Project (Project name), e.g. Project(MailMergeDemo)
Under "Microsoft Word Objects"
Double click "ThisDocument"

and enter the following code

Dim WithEvents app As Application

Private Sub Document_Close()
  Set app = Nothing
End Sub

Private Sub Document_Open()
  Set app = Application
End Sub

Choose the app object from the drop down and then choose the app_MailMergeBeforeRecordMerge event and enter the following code:

Private Sub app_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
  Doc.Variables("MyVariable").Value = Doc.MailMerge.DataSource.DataFields(SomeFieldName).Value

  Doc.Bookmarks("MyBookmarkReference").Range.Text = "this is a test"

  Doc.Fields.Update
End Sub

You will need to close and reopen the document to hook the application object.

Please refer to http://support.microsoft.com/kb/285333 for more information.

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