如何根据用户输入过滤邮件合并源数据?
How to filter mail merge source data according to user input?
四天前我问了一个邮件合并问题并得到了很好的答案,但有人抱怨我会弹出一个确认 SQL 连接的窗口。他们只需单击是,所以在我看来,这不是关键问题。我的问题是,通过从我的宏中连接到 Excel 源文件来消除 SQL 连接确认弹出窗口有多难?只要我拉动整个范围,我就尝试让它工作正常。问题是,我不想要所有的源记录,我只想要一个,即最终用户通过在输入框中键入登录 ID 选择的一个。 LoginID 是电子表格 All_Users$
选项卡中的一列。我一放:
WHERE LoginID = "" + myKey + """ + ",
在 SQL 语句中,我得到错误:发生错误:外部 table 不是预期的格式。
我在这个论坛上查阅了几十篇关于邮件合并的文章,一点一点地构建我的代码。我错过了关于过滤器的最后一部分。
这是我的代码:
Sub MergetoNewDoc()
Dim wrdObj As Object, wrdDoc As Object
Dim strFile As String, myKey As String
Dim mySource As String: mySource = "C:\Users\jtorres\desktop\LetterMemoDB.xlsx"
Set wrdObj = CreateObject("Word.Application")
myKey = InputBox("Enter LoginID:")
With wrdObj
.Visible = False
.DisplayAlerts = False
Set wrdDoc = .Documents.Open("C:\Users\jtorres\desktop\NewLetterTemplate.docx", False, True, False, , , , , , , , False)
With wrdDoc
With .MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.OpenDataSource Name:=mySource, ReadOnly:=True, AddToRecentFiles:=False, _
LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.14.0;User ID=Admin;" & _
"Data Source=mySource;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `All_Users$` WHERE LoginID = "" + myKey + """ + ","
.Execute
End With
.Close 0
End With
End With
Set wrdDoc = Nothing: Set wrdObj = Nothing
End Sub
尝试
SQLStatement:="SELECT * FROM [All_Users$] WHERE LoginID = '" & myKey & "'"
«我有人抱怨弹出窗口来确认 SQL 连接» 更改:
.DisplayAlerts = False
至
.DisplayAlerts = 0 'wdAlertsNone
此后,只有当用户使用 Word 而不是使用宏打开 mailmerge 主文档时,才会出现 SQL 提示。
总的来说,代码可以简化为:
Sub MergetoNewDoc()
Dim wrdObj As Object, wrdDoc As Object
Dim mySource As String: mySource = "C:\Users\jtorres\desktop\LetterMemoDB.xlsx"
Set wrdObj = CreateObject("Word.Application")
With wrdObj
.Visible = True
.DisplayAlerts = 0 'wdAlertsNone
Set wrdDoc = .Documents.Open("C:\Users\jtorres\desktop\NewLetterTemplate.docx", False, True, False)
With wrdDoc
With .MailMerge
.MainDocumentType = 0 'wdFormLetters
.Destination = 0 'wdSendToNewDocument
.SuppressBlankLines = True
.OpenDataSource Name:=mySource, ReadOnly:=True, AddToRecentFiles:=False, _
LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.14.0;User ID=Admin;" & _
"Data Source=mySource;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `All_Users$` WHERE LoginID = """ & InputBox("Enter LoginID:") & """"
.Execute
End With
.Close 0
End With
End With
Set wrdDoc = Nothing: Set wrdObj = Nothing
End Sub
PS:鉴于您没有任何代码可以保存或以其他方式处理输出文档,我看不出使用“.Visible = False”有什么意义。另外,您不会结束 Word 会话。最终结果是在后台出现一个孤立的 Word 会话 运行。
四天前我问了一个邮件合并问题并得到了很好的答案,但有人抱怨我会弹出一个确认 SQL 连接的窗口。他们只需单击是,所以在我看来,这不是关键问题。我的问题是,通过从我的宏中连接到 Excel 源文件来消除 SQL 连接确认弹出窗口有多难?只要我拉动整个范围,我就尝试让它工作正常。问题是,我不想要所有的源记录,我只想要一个,即最终用户通过在输入框中键入登录 ID 选择的一个。 LoginID 是电子表格 All_Users$
选项卡中的一列。我一放:
WHERE LoginID = "" + myKey + """ + ",
在 SQL 语句中,我得到错误:发生错误:外部 table 不是预期的格式。
我在这个论坛上查阅了几十篇关于邮件合并的文章,一点一点地构建我的代码。我错过了关于过滤器的最后一部分。
这是我的代码:
Sub MergetoNewDoc()
Dim wrdObj As Object, wrdDoc As Object
Dim strFile As String, myKey As String
Dim mySource As String: mySource = "C:\Users\jtorres\desktop\LetterMemoDB.xlsx"
Set wrdObj = CreateObject("Word.Application")
myKey = InputBox("Enter LoginID:")
With wrdObj
.Visible = False
.DisplayAlerts = False
Set wrdDoc = .Documents.Open("C:\Users\jtorres\desktop\NewLetterTemplate.docx", False, True, False, , , , , , , , False)
With wrdDoc
With .MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.OpenDataSource Name:=mySource, ReadOnly:=True, AddToRecentFiles:=False, _
LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.14.0;User ID=Admin;" & _
"Data Source=mySource;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `All_Users$` WHERE LoginID = "" + myKey + """ + ","
.Execute
End With
.Close 0
End With
End With
Set wrdDoc = Nothing: Set wrdObj = Nothing
End Sub
尝试
SQLStatement:="SELECT * FROM [All_Users$] WHERE LoginID = '" & myKey & "'"
«我有人抱怨弹出窗口来确认 SQL 连接» 更改:
.DisplayAlerts = False
至
.DisplayAlerts = 0 'wdAlertsNone
此后,只有当用户使用 Word 而不是使用宏打开 mailmerge 主文档时,才会出现 SQL 提示。
总的来说,代码可以简化为:
Sub MergetoNewDoc()
Dim wrdObj As Object, wrdDoc As Object
Dim mySource As String: mySource = "C:\Users\jtorres\desktop\LetterMemoDB.xlsx"
Set wrdObj = CreateObject("Word.Application")
With wrdObj
.Visible = True
.DisplayAlerts = 0 'wdAlertsNone
Set wrdDoc = .Documents.Open("C:\Users\jtorres\desktop\NewLetterTemplate.docx", False, True, False)
With wrdDoc
With .MailMerge
.MainDocumentType = 0 'wdFormLetters
.Destination = 0 'wdSendToNewDocument
.SuppressBlankLines = True
.OpenDataSource Name:=mySource, ReadOnly:=True, AddToRecentFiles:=False, _
LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.14.0;User ID=Admin;" & _
"Data Source=mySource;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `All_Users$` WHERE LoginID = """ & InputBox("Enter LoginID:") & """"
.Execute
End With
.Close 0
End With
End With
Set wrdDoc = Nothing: Set wrdObj = Nothing
End Sub
PS:鉴于您没有任何代码可以保存或以其他方式处理输出文档,我看不出使用“.Visible = False”有什么意义。另外,您不会结束 Word 会话。最终结果是在后台出现一个孤立的 Word 会话 运行。