gpt4 book ai didi

excel - 使用 Excel VBA 创建 Word 应用程序 : Run-time error '429' : ActiveX component can't create object

转载 作者:行者123 更新时间:2023-12-01 18:18:56 24 4
gpt4 key购买 nike

我尝试使用 Excel VBA 保存 Word 文档,但收到错误

"ActiveX component can't create object."

当我调试时,错误来自以下行: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

最佳答案

我在从 Windows 7 升级到 10 时遇到了问题,因为我随身携带了一堆 VBA 脚本。仍然不确定错误的根本原因是什么,但同时这段代码对我有用。这是一种解决方法,它限制了 Word(或 Outlook/Excel)已处于打开(手动)状态的需要,但如果您设置了引用,则应该允许您的脚本运行。只需将 "CreateObject(" 更改为 "GetObject(, ")。这将告诉系统使用已打开的窗口。

要使用的完整代码是:

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

关于excel - 使用 Excel VBA 创建 Word 应用程序 : Run-time error '429' : ActiveX component can't create object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17327327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com