我尝试使用Excel VBA保存Word文档,但收到错误消息
"ActiveX组件无法创建对象。“
当我调试时,错误来自行:Set wrdApps = CreateObject("Word.Application")
。
它正在工作,然后它开始给我这个错误。
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
发布于 2018-07-12 07:28:29
在从Windows 7升级到10时,我随身携带了大量的VBA脚本,遇到了一个问题。仍然不确定错误的根本原因是什么,但与此同时,这段代码对我来说是有效的。这是一种解决方法,可以限制已经处于打开(手动)状态的Word (或Outlook/Excel)的需要,但如果您设置了引用,则应允许脚本运行。只需将"CreateObject("
更改为"GetObject(, "
。这将告诉系统使用已经打开的窗口。
要使用的完整代码为:
Dim wrdApps As Object
Dim wrdDoc As Object
Set wrdApps = GetObject(, "Word.Application")
发布于 2019-12-06 00:19:30
我最近在我写的一些代码中遇到了这种情况。突然(在成功运行了几个月之后),我得到了同样的运行时错误'429‘。这发生在两台不同的计算机上,一台是我在几个月前编写和测试代码的计算机,另一台是实际使用该工具的人的计算机。尽管我在我的机器上使用了原始文件,而用户已经成功地使用了他的副本几个月,但还是发生了这种情况,所以我不相信两台不同机器上的两个单独的文件都以相同的方式损坏了。话虽如此,我也不能很好地解释为什么会发生这种情况。我的代码也有类似的代码行:
Dim objFSO as Object
Set objFSO = CreateObj("Scripting.FileSystemObject")
我包含了对脚本库的引用,并且在过去成功地做到了这一点。
我能够通过将该对象上的延迟绑定更改为早期绑定来修复该问题:
Dim objFSO as New Scripting.FileSystemObject
我把所有东西都切换到早期绑定已经有一段时间了,但这是一些遗留代码。可以在这里找到关于两者之间差异的简短解释:https://excelmacromastery.com/vba-dictionary/#Early_versus_Late_Binding
我不完全确定为什么这样做解决了这个问题,但希望它能在未来帮助其他人解决类似的问题。
发布于 2013-09-30 17:53:06
wrdDoc是否已初始化?您是否正在尝试在设置对象之前使用wrdDoc?
wrdApps.documents.Add wrdDoc.FullName
Set wrdDoc = wrdApps.documents.Open(ThisWorkbook.Worksheets(3).Range("f" & i).Value)
第一行是否应该像注释中那样为ActiveDocument.FullName?所以:
wrdApps.documents.Add ActiveDocument.FullName
https://stackoverflow.com/questions/17327327
复制相似问题