从Excel向发布者邮件合并添加过滤器

问题描述 投票:1回答:1

我在Excel中有一个宏,该宏在Publisher中运行邮件合并。

如何将此过滤器添加到当前代码?

sheet =“ ALL Sections $”,colIndex = icol,条件=“ part1name”

在Publisher中运行邮件合并的代码:

    Dim strWorkbookName As String
    Dim pubSource As Object
    Dim mrgMain As MailMerge
    Dim appPub As Object
    Dim FileLink As String

    FileLink = [Rank1MailMerge].Value
    Set appPub = CreateObject("Publisher.Application")
    strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
    Set pubSource = appPub.Open(FileLink)
    Set mrgMain = pubSource.MailMerge

    pubSource.MailMerge.OpenDataSource _
      bstrDataSource:=strWorkbookName, _
      bstrTable:="ALL Sections$", _
      fNeverPrompt:=True

    With mrgMain.DataSource
        .FirstRecord = pbDefaultFirstRecord
        .LastRecord = pbDefaultLastRecord
    End With
    mrgMain.Execute False, pbMergeToNewPublication
    End Sub
excel vba mailmerge publisher
1个回答
1
投票

[已解决],我终于弄清楚了如何应用我的过滤器以及我在此过程中发现的其他一些问题-几乎没有关于发布者邮件的任何信息。

代码:

Sub MergeToPub ()
Dim strWorkbookName As String
Dim pubSource As Object
Dim mrgMain As MailMerge
Dim appPub As New Publisher.Application
Dim FileLink As String

  strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
  FileLink = [MailMergePub].Value
  appPub.ActiveWindow.Visible = True
  Set pubSource = appPub.Open(FileLink)
  Set mrgMain = pubSource.MailMerge

'before i added this next line of code, for some reason 
'it added the same data source twice and merged duplicate results
 If pubSource.MailMerge.DataSource.Name = strWorkbookName Then GoTo ContinueCode


    pubSource.MailMerge.OpenDataSource _
        bstrDataSource:=strWorkbookName, _
        bstrTable:="Sheet1$", _
        fNeverPrompt:=True

ContinueCode:
'this adds two filters
    With mrgMain.DataSource
        .Filters.Add Column:="Column1", _
           Comparison:=msoFilterComparisonEqual, _
           Conjunction:=msoFilterConjunctionAnd, _
           bstrCompareTo:="Name"

      .Filters.Add Column:="Column2", _
           Comparison:=msoFilterComparisonNotEqual, _
           Conjunction:=msoFilterConjunctionAnd, _
           bstrCompareTo:="yes"
           .ApplyFilter

        .FirstRecord = pbDefaultFirstRecord
        .LastRecord = pbDefaultLastRecord
    End With

mrgMain.Execute False, pbMergeToNewPublication
pubSource.Close
 Set appPub = Nothing
 Set pubSource = Nothing
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.