【问题标题】:Perform Mail Merge And Not Show Word执行邮件合并而不显示单词
【发布时间】:2018-01-09 16:29:59
【问题描述】:

我正在从 Excel 执行邮件合并,这完全符合我的需要。我的问题是我想对用户隐藏单词,而这并没有发生。我最终在屏幕上出现了一个我不想要的空单词实例。

这是我的语法 - 为什么我无法在进程完成后完全隐藏和关闭单词?

    Dim wdapp As Word.Application, wdDoc As Word.Document, wdMaiMerge As Word.MailMerge
'Setting refs
Set wdapp = CreateObject("Word.Application")
Set wdDoc = wdapp.Documents.Open(wdpath)
Set wdMailMerge = wdDoc.MailMerge
'hiding display from user
wdapp.Visible = False
'Setting mail merge
With wdMailMerge
 .OpenDataSourcexxxx, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False
 .Execute
End With
'Finishing
Set wdapp = Nothing
wdapp.Quit

【问题讨论】:

  • 您应该先退出应用程序 (wdapp.Quit),然后再发布对它的引用 (Set wdapp = Nothing)。
  • @destination-data - 仍然给我留下空白的单词窗口。并且用户可以看到单词
  • 您的代码是否包含任何错误处理?这行wdapp.Visible = False之后wdapp.Visible的值是多少?
  • @destination-data - 没有错误处理...仍在尝试解决错误。如果我在该行之后直接执行 debug.print wdapp.Visible 它会打印 false
  • 到@destination-data 点,删除它实际执行邮件合并的部分,看看您是否留下了一个悬挂的Word 实例。另外,在wdApp.Quit 之前添加wdDoc.Close False 看看是否有帮助。

标签: vba excel excel-2013 word-2013


【解决方案1】:

您不会告诉 Word 使用什么查询或如何处理您使用后用于邮件合并的文档或邮件合并输出!而且,如果您要打开的文档是 mailmerge 主文档,那么您的代码将在此时挂起 - 您需要抑制它并自己提供所有 SQL 代码。例如:

Sub MailMerge()
'Note: A VBA Reference to the Word Object Model is required, via Tools|References
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim strWorkbookName As String: strWorkbookName = ThisWorkbook.FullName
With wdApp
  .Visible = False
  'Disable alerts to prevent an SQL prompt
  .DisplayAlerts = wdAlertsNone
  'Open the mailmerge main document
  Set wdDoc = .Documents.Open(Filename:=ThisWorkbook.Path & "\MailMergeMainDocument.docx", _
    ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    With .MailMerge
      'Define the mailmerge type
      .MainDocumentType = wdFormLetters
      'Define the output
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      'Connect to the data source
      .OpenDataSource Name:=strWorkbookName, ReadOnly:=True, _
        LinkToSource:=False, AddToRecentFiles:=False, Format:=wdOpenFormatAuto, _
        Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "User ID=Admin;Data Source=strWorkbookName;" & _
        "Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
        SQLStatement:="SELECT * FROM `Sheet1$`", SubType:=wdMergeSubTypeAccess
      With .DataSource
        .FirstRecord = wdDefaultFirstRecord
        .LastRecord = wdDefaultLastRecord
      End With
      'Excecute the merge
      .Execute
      With wdApp.ActiveDocument
        'What do you want to do with the output document??? For example:
        .SaveAs2 Filename:=ThisWorkbook.Path & "\MailMergeOutputDocument.docx", _
          FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        ' and/or:
        .SaveAs Filename:=ThisWorkbook.Path & "\MailMergeOutputDocument.pdf", _
          FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        'Close the output document
        .Close False
      End With
      'Disconnect from the data source
      .MainDocumentType = wdNotAMergeDocument
    End With
    'Close the mailmerge main document
    .Close False
  End With
  'Restore the Word alerts
  .DisplayAlerts = wdAlertsAll
  'Quit Word
  .Quit
End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2019-06-02
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多