简体   繁体   English

使用 Excel VBA 创建 Word 应用程序:运行时错误“429”:ActiveX 组件无法创建对象

[英]Creating Word Application using Excel VBA: Run-time error '429': ActiveX component can't create object

I am trying to save Word docs using Excel VBA, but I get the error我正在尝试使用 Excel VBA 保存 Word 文档,但出现错误

"ActiveX component can't create object." “ActiveX 组件无法创建对象。”

When I debug, the error comes from the line: Set wrdApps = CreateObject("Word.Application") .当我调试时,错误来自以下行: Set wrdApps = CreateObject("Word.Application")

It was working, then it started giving me this error.它正在工作,然后它开始给我这个错误。

Sub saveDoc()

Dim i As Integer
For i = 1 To 2661:
    Dim fname As String
    Dim fpath As String

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    fname = ThisWorkbook.Worksheets(3).Range("H" & i).Value
    fpath = ThisWorkbook.Worksheets(3).Range("G" & i).Value

    Dim wrdApps As Object
    Dim wrdDoc As Object

    Set wrdApps = CreateObject("Word.Application")

    'the next line copies the active document- the ActiveDocument.FullName 
    ' is important otherwise it will just create a blank document
    wrdApps.documents.Add wrdDoc.FullName

    Set wrdDoc = wrdApps.documents.Open(ThisWorkbook.Worksheets(3).Range("f" & i).Value)
    ' do not need the Activate, it will be Activate
    wrdApps.Visible = False  

    ' the next line saves the copy to your location and name
    wrdDoc.SaveAs "I:\Yun\RTEMP DOC & PDF\" & fname

    'next line closes the copy leaving you with the original document
    wrdDoc.Close

    On Error GoTo NextSheet:
NextSheet:
    Resume NextSheet2
NextSheet2:
Next i

With Application
   .DisplayAlerts = True
   .ScreenUpdating = True
   .EnableEvents = True
End With

End Sub

I had an issue when upgrading from Windows 7 to 10 when bringing my hoard of VBA scripts with me.带着我的 VBA 脚本库从 Windows 7 升级到 10 时,我遇到了问题。 Still not sure what the root cause of the error is, but in the mean time this piece of code worked for me.仍然不确定错误的根本原因是什么,但同时这段代码对我有用。 This is a workaround that limits the need to have Word (or Outlook/Excel) already in open (manually) state, but should allow your script to run if you have your references set.这是一种解决方法,它限制了 Word(或 Outlook/Excel)已经处于打开(手动)状态的需要,但如果您设置了引用,则应该允许您的脚本运行。 Just change "CreateObject(" to "GetObject(, " . This will tell the system to use an already open window.只需将"CreateObject("更改为"GetObject(, " 。这将告诉系统使用已经打开的窗口。

The complete code to use would be:要使用的完整代码是:

Dim wrdApps As Object
Dim wrdDoc As Object
Set wrdApps = GetObject(, "Word.Application")

Is wrdDoc initialised? wrdDoc 是否已初始化? Are you trying to use wrdDoc before the object has been Set?您是否在设置对象之前尝试使用 wrdDoc?

wrdApps.documents.Add wrdDoc.FullName
Set wrdDoc = wrdApps.documents.Open(ThisWorkbook.Worksheets(3).Range("f" & i).Value)

Should the first line be ActiveDocument.FullName as in the comments?第一行应该是注释中的 ActiveDocument.FullName 吗? So:所以:

wrdApps.documents.Add ActiveDocument.FullName

Check that you have the Microsoft Excel Object Library and the Microsoft Office Object Library ticked in Tools > References and that they have been registered.检查您是否在工具 > 参考中勾选了 Microsoft Excel 对象库和 Microsoft Office 对象库,并且它们已被注册。

If they are ticked, you may need to run Detect and Repair from the Excel Help menu to make sure that the Office installation hasn't corrupted in any way.如果它们被勾选,您可能需要从 Excel 帮助菜单运行检测和修复以确保 Office 安装没有以任何方式损坏。

I recently had this happen to some code I had written.我最近在我写的一些代码中发生了这种情况。 Out of nowhere (after running successfully for a few months), I would get the same Runtime Error '429'.突然(成功运行几个月后),我会得到相同的运行时错误“429”。 This happened on two separate computers, the one I wrote and tested the code on months prior and the computer of the person who actually used the tool.这发生在两台不同的计算机上,我在几个月前编写并测试了代码的计算机以及实际使用该工具的人的计算机。 It happened even though I used the original file on my machine and the user had been using his copy successfully for a few months, so I'm not convinced two separate files on two separate machines both got corrupted in the same manner.即使我在我的机器上使用了原始文件并且用户已经成功使用了他的副本几个月,但它还是发生了,所以我不相信两台不同机器上的两个单独文件都以相同的方式损坏。 With that being said, I don't have a good explanation of why this occurred.话虽如此,我对为什么会发生这种情况没有很好的解释。 Mine would happen on a similar line of code:我的将发生在类似的代码行上:

Dim objFSO as Object
Set objFSO = CreateObj("Scripting.FileSystemObject")

I had the reference to the scripting library included and had done this successfully in the past.我参考了脚本库,并且在过去成功完成了这项工作。

I was able to fix the problem by changing from late to early binding on that object:我能够通过将该对象从后期绑定更改为早期绑定来解决该问题:

Dim objFSO as New Scripting.FileSystemObject

I have been switching everything over to early binding for some time now but this was some legacy code.一段时间以来,我一直将所有内容切换到早期绑定,但这是一些遗留代码。 A nice short little explanation on the difference between the two can be found here: https://excelmacromastery.com/vba-dictionary/#Early_versus_Late_Binding可以在此处找到有关两者之间差异的简短说明: https : //excelmacromastery.com/vba-dictionary/#Early_versus_Late_Binding

I'm not entirely certain why that fixed the problem, but hopefully it will help others in the future with similar issues.我不完全确定为什么会解决这个问题,但希望它会在未来帮助其他人解决类似问题。

It's seems to be linked to the presence of a second "ThisWorkbook" object, in my case.就我而言,它似乎与第二个“ThisWorkbook”对象的存在有关。

Excel VBA Project has generated multiple Workbook objects Excel VBA 项目已生成多个 Workbook 对象

No other solution found that the one in this link.没有其他解决方案找到此链接中的解决方案。 I didn't try it myself, though.不过我自己没试过。

In my case, the workbook appeared to be corrupted.就我而言,工作簿似乎已损坏。 Simply assigning a worksheet to a variable was throwing the error.简单地将工作表分配给变量会引发错误。

Dim ws as Worksheet
Set ws = ThisWorkbook.Worksheets("Data")

The workbook came directly from the client, so I'm not sure what they did with it, or how old it was but all the references appeared to be selected.工作簿直接来自客户,所以我不确定他们用它做了什么,或者它有多久了,但所有参考似乎都被选中了。 Every other workbook with the same code was working correctly.具有相同代码的所有其他工作簿都正常工作。

So to resolve the issue I copied the worksheets and modules to a new workbook and it worked without any issues.所以为了解决这个问题,我将工作表和模块复制到一个新的工作簿中,并且没有任何问题。

Might not always be the best solution, but if you haven't got any worksheets and modules, then it's pretty easy.可能并不总是最好的解决方案,但如果您没有任何工作表和模块,那么这很容易。

Try this one.. i've encountered this a lot of time...试试这个……我遇到过很多次了……

so I just run my excel by searching it (located on taskbar), then right click then "run as administrator" or if you already created the excel file, open it from file>open>browse.所以我只是通过搜索它(位于任务栏上)来运行我的 excel,然后右键单击然后“以管理员身份运行”,或者如果您已经创建了 excel 文件,请从文件>打开>浏览中打开它。 avoid just double clicking the excel file to open directly.避免双击excel文件直接打开。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA 编写内嵌 VBScript - 运行时错误“429”:ActiveX 组件无法创建对象 - Excel VBA Writing in-line VBScript - Run-time error '429': ActiveX component can't create object 创建Outlook对象会生成-运行时错误'429':ActiveX组件无法创建对象 - Creating Outlook object generates - Run-time error '429': ActiveX component can't create object 从 Excel 发送邮件 - 运行时错误“429”:ActiveX 组件无法创建对象 - Sending mails from Excel - Run-time error '429': ActiveX component can't create object 运行时错误“ 429” activex组件无法创建对象 - run-time error '429' activex component can't create object 使用任务计划程序在 Excel 中运行 selenium v​​ba 宏时,如何修复“运行时错误 429:ActiveX 组件无法创建对象”错误? - How to fix "Run time error 429: ActiveX Component Can't Create Object" Error when using Task Scheduler to run a selenium vba macro in Excel? VBA:Acrobat运行时错误429; ActiveX组件无法创建对象 - VBA: Acrobat Run time error 429; ActiveX component can't create object Excel VBA ActiveX'429'运行时错误 - Excel VBA ActiveX '429' Run-time error ActiveX 组件无法创建 object - 429 - ActiveX component can't create object - 429 脚本在调试模式下工作,但在正常运行下不工作-错误代码:429(ActiveX组件无法创建对象) - Script working in Debug Mode but not in Normal Run - Error Code: 429(ActiveX Component Can't Create Object') activex组件无法使用word.application创建对象错误 - activex component can't create object error with word.application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM