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

Error when saving individual docs from a mail merge

我一直在使用 VBA 代码将邮件合并中的所有信件单独保存到指定文件夹中。它以前一直在处理我正在尝试执行的文档,但现在它只是保存第一个文档,然后出现错误说明:

运行-时间错误'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

你必须像这样设置你的对象:

Set DocResult = nothing