首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行时错误'5852‘请求的对象不可用。.Destination = wdSendToNewDocument的问题

运行时错误'5852‘请求的对象不可用。.Destination = wdSendToNewDocument的问题
EN

Stack Overflow用户
提问于 2021-07-29 04:26:44
回答 1查看 270关注 0票数 0

因此,我一直在尝试使用下面的宏将邮件合并的文档拆分成单独的文档。当我运行宏时,我收到“运行时错误'5852‘请求的对象不可用”。使用调试操作时,问题突出显示为.Destination = wdSendToNewDocument

我认为问题可能是文件位于我的OneDrive上,但在将文件移动到本地驱动器后,我收到了相同的问题。对如何解决此错误的任何见解都将是有帮助的。

如果需要更多信息,请让我知道,我很乐意尽我所能回答。

参考代码:

代码语言:javascript
复制
   Sub MailMergeToDoc()
'
' MailMergeToDoc Macro
' Collects the results of the mail merge in a document
'
' Sourced from: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
Set MainDoc = ActiveDocument
With MainDoc
  StrFolder = .Path & "\"
  With .MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    On Error Resume Next
    For i = 1 To .DataSource.RecordCount
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        If Trim(.DataFields("Last_Name")) = "" Then Exit For
        'StrFolder = .DataFields("Folder") & "\"
        StrName = .DataFields("Last_Name") & "_" & .DataFields("First_Name")
      End With
      On Error GoTo NextRecord
      .Execute Pause:=False
      For j = 1 To Len(StrNoChr)
        StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
      Next
      StrName = Trim(StrName)
      With ActiveDocument
        'Add the name to the footer
        '.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertBefore StrName
        .SaveAs FileName:=StrFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        ' and/or:
        .SaveAs2 FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close SaveChanges:=False
      End With
NextRecord:
    Next i
  End With
End With
Application.ScreenUpdating = True
End Sub
EN

回答 1

Stack Overflow用户

发布于 2021-07-29 06:43:46

这是非常基本的故障排除。你不能只复制代码而不理解它在做什么。

当您尝试运行邮件合并时,您的MailMerge对象不存在。

您需要在Word文档中创建邮件合并第一个-只需使用向导-该对象将被神奇地填充。那么你将不得不前进到你的下一个错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68566957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档