访问中的mailmerge vba在一个.xls文件上调用一个选择表弹出窗口,只有一个工作表
该代码应该生成标签,该标签将用于在深水潜水期间收集的标本。
Sub MailMergeSpecimenLabels()
Dim pathMergeTemplates As String
Dim pathMergeLabels As String
Dim pathMergeTemp As String
Dim outfilename As String
Dim infilename As String
Dim templatefilename As String
Dim msg As String
'Get the word template from the templates folder
pathMergeTemplates = CurrentProject.Path & "\templates\"
pathMergeLabels = CurrentProject.Path & "\completed\"
pathMergeTemp = CurrentProject.Path & "\temp\"
'Export the data using a Macro
templatefilename = pathMergeTemplates & "Primary_Specimens.docx"
infilename = pathMergeTemp & "SpecimenLabels.xls"
outfilename = pathMergeLabels & "PrimarySpecLabels_" & Format(Now(), "yyyymmddmms") & ".docx"
DoCmd.RunMacro ("ExportPrimarySpecsforLabels")
' Access has built the .xls file.
'Now the mail merge doc gets opened
Dim appWord As Object
Dim docWord As Object
Set appWord = CreateObject("Word.Application")
'Open the template in the templates folder
Set docWord = appWord.Documents.Add(templatefilename, Visible:=False)
'Now we can mail merge without involving the database
With docWord.MailMerge
.OpenDataSource Name:=infilename, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False
.Destination = 0
.Execute Pause:=False
End With
appWord.ActiveDocument.SaveAs outfilename
msg = "Specimen Labels are generated. Would you like to open the label file?"
DoCmd.RunMacro "MarkPrinted"
Response = MsgBox(msg, Style)
If Response = 1 Then
appWord.Documents.Open filename:=outfilename & "", ReadOnly:=False
End If
appWord.Quit savechanges:=False
Set docWord = Nothing
Set appWord = Nothing
Finally:
Exit Sub
Hell:
MsgBox Err.Description & " " & Err.Number, vbExclamation
On Error Resume Next
appWord.Quit savechanges:=False
Set docWord = Nothing
Set appWord = Nothing
Resume Finally
End Sub
当mailmerge.ecute
发生时,弹出窗口(我只能查看是否转到任务管理器)会中断代码要求我选择表。我该如何避免这种情况? .xls文件中只有一个表。
我认为当我重建模板并在设置数据源时在此处找到该精选的表弹出窗口时,我发现了问题,但是它不起作用。
This code is supposed to generate labels that will be used for labeling specimens collected during deep water dives.
Sub MailMergeSpecimenLabels()
Dim pathMergeTemplates As String
Dim pathMergeLabels As String
Dim pathMergeTemp As String
Dim outfilename As String
Dim infilename As String
Dim templatefilename As String
Dim msg As String
'Get the word template from the templates folder
pathMergeTemplates = CurrentProject.Path & "\templates\"
pathMergeLabels = CurrentProject.Path & "\completed\"
pathMergeTemp = CurrentProject.Path & "\temp\"
'Export the data using a Macro
templatefilename = pathMergeTemplates & "Primary_Specimens.docx"
infilename = pathMergeTemp & "SpecimenLabels.xls"
outfilename = pathMergeLabels & "PrimarySpecLabels_" & Format(Now(), "yyyymmddmms") & ".docx"
DoCmd.RunMacro ("ExportPrimarySpecsforLabels")
' Access has built the .xls file.
'Now the mail merge doc gets opened
Dim appWord As Object
Dim docWord As Object
Set appWord = CreateObject("Word.Application")
'Open the template in the templates folder
Set docWord = appWord.Documents.Add(templatefilename, Visible:=False)
'Now we can mail merge without involving the database
With docWord.MailMerge
.OpenDataSource Name:=infilename, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False
.Destination = 0
.Execute Pause:=False
End With
appWord.ActiveDocument.SaveAs outfilename
msg = "Specimen Labels are generated. Would you like to open the label file?"
DoCmd.RunMacro "MarkPrinted"
Response = MsgBox(msg, Style)
If Response = 1 Then
appWord.Documents.Open filename:=outfilename & "", ReadOnly:=False
End If
appWord.Quit savechanges:=False
Set docWord = Nothing
Set appWord = Nothing
Finally:
Exit Sub
Hell:
MsgBox Err.Description & " " & Err.Number, vbExclamation
On Error Resume Next
appWord.Quit savechanges:=False
Set docWord = Nothing
Set appWord = Nothing
Resume Finally
End Sub
When the mailmerge.execute
happens, a popup window (that I can only see if I go to Task Manager) interrupts the code asking me to Select Table. How can I avoid this? There is only one sheet in the .xls file.
I thought I found the problem when I rebuilt the template and got that Select Table popup there when setting my data source, but it did not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从另一个应用程序运行mailmerge时,您需要禁用用于MailMerge的文档中的任何MailMerge警报 - 杀死该文档中的MailMerge参数 - 然后重新确定代码中的MailMermerge参数。因此:
When running a mailmerge from another application, you need to disable any mailmerge alerts in the document being used for the mailmerge - which kills the mailmerge parameters in that document - then reinstate the mailmerge parameters in code. Hence: