从 Excel 向 Publisher 邮件合并添加过滤器
Add filter to Publisher mail merge from Excel
我在 Excel 中有一个宏,运行 是 Publisher 中的邮件合并。
如何将此过滤器添加到当前代码中?
sheet="ALL Sections$", colIndex= icol, criteria="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
[已解决] 我终于想出了如何应用我的过滤器以及我在此过程中发现的其他一些问题 - 那里几乎没有关于发布者邮件合并的任何信息。
代码:
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
我在 Excel 中有一个宏,运行 是 Publisher 中的邮件合并。
如何将此过滤器添加到当前代码中?
sheet="ALL Sections$", colIndex= icol, criteria="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
[已解决] 我终于想出了如何应用我的过滤器以及我在此过程中发现的其他一些问题 - 那里几乎没有关于发布者邮件合并的任何信息。
代码:
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