【问题标题】:How to fix "Execution Error 429 : ActiveX can't create object" error in VBA如何修复 VBA 中的“执行错误 429:ActiveX 无法创建对象”错误
【发布时间】:2019-06-11 15:14:46
【问题描述】:

下面的这个 Excel 宏应该将所有 .Docx 转换为所选文件夹中的 .Pdf

这是为我提供错误代码 429 的代码行,但几个小时前,同一行代码还在工作。

Documents.Open (filePath & currFile) 'Error Code 429

这里是完整的宏代码

Sub ConvertDocxInDirToPDF()

Dim filePath As String
Dim currFile As String

filePath = ActiveWorkbook.Path & "\"
MsgBox filePath
currFile = Dir(filePath & "*.docx")

Do While currFile <> ""

    Documents.Open (filePath & currFile) 'Error Code 429

    Documents(currFile).ExportAsFixedFormat _
        OutputFileName:=filePath & Left(currFile, Len(currFile) - Len(".docx")) & ".pdf", _
        ExportFormat:=17
    Documents(currFile).Close

    currFile = Dir()

Loop

Application.ScreenUpdating = True

End Sub

有没有一种简单的方法可以使这个宏工作并修复这个错误。

最好的问候。

【问题讨论】:

标签: excel vba export-to-pdf


【解决方案1】:

Documents.Open是Documents对象的一个​​方法,需要“MS Word对象库”才能起作用,而不是显式引用一个word对象:

这是什么意思?如果选中了Microsoft Word 1X.0 引用(VBE>Extras>Libraries),那么下面的代码可以正常工作:

Sub TestMe()

    Dim filePath As String
    filePath = ThisWorkbook.Path & "\"
    Dim currFile As String
    currFile = Dir(filePath & "*.docx")

    Dim wrdDoc As Object
    Documents.Open filePath & currFile

End Sub

如果“MS Word 对象库”没有被引用,那么通过后期绑定它仍然可以被引用到对象。 (后期绑定是CreateObject("Word.Application")):

Sub TestMe()

    Dim filePath As String
    filePath = ThisWorkbook.Path & "\"
    Dim currFile As String
    currFile = Dir(filePath & "*.docx")

    Dim wrdApps As Object
    Set wrdApps = CreateObject("Word.Application")
    wrdApps.Documents.Open (filePath & currFile)

End Sub

如果需要,Documents.Open 可能会返回一个文档对象:

Sub TestMe()

    Dim filePath As String
    filePath = ThisWorkbook.Path & "\"
    Dim currFile As String
    currFile = Dir(filePath & "*.docx")

    Dim wrdApps As Object
    Set wrdApps = CreateObject("Word.Application")

    Dim wrdDoc As Object
    Set wrdDoc = wrdApps.Documents.Open(filePath & currFile)

End Sub

【讨论】:

  • 谢谢@Vityata,谢谢它解决了我的问题,你能解释一下为什么Documents.open 可以工作,而现在不行。我试图理解,但我想我在这里错过了一些东西。问候
  • @Dorian - 不客气。我扩大了答案,解释了从 Excel 中使用 Documents.Open 所需的库,而不返回对象。
  • Helle,现在我得到了同样的错误,但在这里:Set wrdApps = CreateObject("Word.Application") 当我按下 F5 时奇怪的事实在错误消息之后代码运行良好我该如何解决这个问题?
  • @Dorian - 确实很奇怪。尝试其中之一 - 在新的 word 文件中创建文件。然后尝试其中之一 - stackoverflow.com/questions/40343021/…
  • @Viyata ,谢谢我刚刚通过使用 docx 在每个 subb 末尾杀死 Word 进程来避开这个问题。谢谢大家
猜你喜欢
  • 1970-01-01
  • 2013-04-13
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 2016-04-24
  • 1970-01-01
  • 2021-08-04
相关资源
最近更新 更多