从邮件合并保存单个文档时出错

时间:2019-07-18 11:16:04

标签: vba ms-word mailmerge

我一直在使用VBA代码将所有字母从邮件合并中单独保存到指定的文件夹中。到目前为止,它一直以来都可以与我要处理的文档一起使用,因为它现在是onyl保存了第一个文档,然后提出了一个错误说明:

运行时错误'5825'对象已删除

当我去调试时,它会在底部显示“ DocResult.Close False”附近的那一行高亮显示

我该如何解决?

尝试将其更改为True或完全删除行,但不能解决问题。每个文档都很大,因此保存大约需要30秒

Dim WithEvents wdapp As Application
Dim bCustomProcessing As Boolean

Private Sub Document_Open()

Set wdapp = Application
bCustomProcessing = False
ThisDocument.MailMerge.DataSource.ActiveRecord = 1
ThisDocument.MailMerge.ShowWizard 1
With ActiveDocument.MailMerge
   If .MainDocumentType = wdFormLetters Then
       .ShowSendToCustom = "Custom Letter Processing"
   End If
End With

End Sub
Private Sub wdapp_MailMergeWizardSendToCustom(ByVal Doc As Document)

bCustomProcessing = True
Doc.MailMerge.Destination = wdSendToNewDocument
With Doc.MailMerge
    For rec = 1 To .DataSource.RecordCount
        .DataSource.ActiveRecord = rec
        .DataSource.FirstRecord = rec
        .DataSource.LastRecord = rec
        .Execute
    Next
End With

MsgBox "Merge Finished"
End Sub


Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)
If bCustomProcessing = True Then
    With Doc.MailMerge.DataSource.DataFields
        sFirmFileName = .Item(44).Value ' First Column of the data - CHANGE
    End With
    DocResult.SaveAs "xxx\" & sFirmFileName & ".doc", wdFormatDocument
     ' Path and File Name to save. can use other formats like wdFormatPDF too
    DocResult.Close False
End If
End Sub

1 个答案:

答案 0 :(得分:0)

您必须将对象设置为像这样的东西:

Set DocResult = nothing