从excel执行Publisher邮件合并

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

我在excel中有一个宏,该宏在发布服务器中运行邮件合并。我想为其添加过滤器。任何人都知道如何将其添加到当前代码中-

我要添加工作表=“ ALL Sections $”,colIndex = icol,criteria =“ part1name”的过滤器

谢谢

到目前为止我要在发布者中运行邮件合并的代码:

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个回答
0
投票

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

代码:

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.