问题描述
我正在尝试使用Excel VBA保存Word文档,但出现错误
I am trying to save Word docs using Excel VBA, but I get the error
当我调试时,错误来自以下行: Set wrdApps = CreateObject( Word.Application )
。
When I debug, the error comes from the line: 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
推荐答案
从Windows 7升级到10时遇到问题,因为我随身携带了VBA脚本。
仍然不确定错误的根本原因是什么,但是与此同时这段代码对我有用。
这是一种变通方法,它限制了Word(或Outlook / Excel)已经处于打开(手动)状态的需要,但是如果设置了引用,则应允许运行脚本。
只需将 CreateObject(
更改为 GetObject(,
。这将告诉系统使用
I had an issue when upgrading from Windows 7 to 10 when bringing my hoard of VBA scripts with me.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.Just change "CreateObject("
to "GetObject(, "
. This will tell the system to use an already open window.
要使用的完整代码为:
Dim wrdApps As Object
Dim wrdDoc As Object
Set wrdApps = GetObject(, "Word.Application")
这篇关于使用Excel VBA创建Word应用程序:运行时错误“ 429”:ActiveX组件无法创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!