简体   繁体   English

Word MailMerge部分范围

[英]Word MailMerge partial range

I have a VB.NET VSTO AddIn for Word. 我有一个Word的VB.NET VSTO加载项。

In this project, I need to execute a MailMerge operation, but only for a partial range of the MergeSource. 在这个项目中,我需要执行一个MailMerge操作,但是只对MergeSource的部分范围执行。

In this example, let's assume the MergeSource contains 100 recipients. 在此示例中,假设MergeSource包含100个收件人。 I want to only include recipient 11-20. 我只想包含收件人11-20。

Normally I do the following, but this includes ALL recipients. 通常,我会执行以下操作,但这包括所有收件人。

Sub ExecuteMerge(ByRef Doc As Word.Document)
    Doc.MailMerge.Execute(False)
End Sub

What I would like to be able to do, is something like this: 我想做的是这样的:

Sub ExecuteMerge(ByRef Doc As Word.Document, Optional StartPos As Integer = 0, Optional EndPos As Integer = 0)
    If StartPos > 0 AndAlso StartPos <= Doc.MailMerge.DataSource.RecordCount Then
        Doc.MailMerge.StartPosition = StartPos
    Else
        Doc.MailMerge.StartPosition = 1
    End If

    If EndPos > StartPos AndAlso EndPos <= Doc.MailMerge.DataSource.RecordCount Then
        Doc.MailMerge.EndPosition = EndPos
    Else
        Doc.MailMerge.EndPosition = Doc.MailMerge.DataSource.RecordCount
    End If
    Doc.MailMerge.Execute(False)
End Sub

Note: .StartPosition and .EndPosition is just pseudo-code, it doesn't exist as a property. 注意: .StartPosition.EndPosition只是伪代码,它不作为属性存在。

This is what I am looking for. 这就是我想要的。 How do I set the range of recipients to include in the merge? 如何设置要包含在合并中的收件人范围?

Word must be able to do this, since when I perform MailMerge manually, I get this dialog: Word必须能够执行此操作,因为当我手动执行MailMerge时,会出现以下对话框: MailMerge对话框11-20

I figured it out, you can set .FirstRecord and .LastRecord on the DataSource . 我想通了,您可以在DataSource上设置.FirstRecord.LastRecord

Like so: 像这样:

Sub ExecuteWordMailMerge(ByRef Doc As Word.Document, Optional StartPos As Integer = 0, Optional EndPos As Integer = 0)
    If StartPos > 0 AndAlso StartPos <= Doc.MailMerge.DataSource.RecordCount Then
        Doc.MailMerge.DataSource.FirstRecord = StartPos
    Else
        Doc.MailMerge.DataSource.FirstRecord = 1
    End If

    If EndPos > StartPos AndAlso EndPos <= Doc.MailMerge.DataSource.RecordCount Then
        Doc.MailMerge.DataSource.LastRecord = EndPos
    Else
        Doc.MailMerge.DataSource.LastRecord = Doc.MailMerge.DataSource.RecordCount
    End If
    Doc.MailMerge.Execute(False)
End Sub

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

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