[英]activex component can't create object error with word.application
I am using the below mentioned code to copy some values in word and then copying it back to excel. 我正在使用下面提到的代码在word中复制一些值,然后将其复制回excel。
But getting the 但是得到
runtime error 429 Activex component can't create object.
运行时错误429 Activex组件无法创建对象。
This same piece of code was working before I formatted my system. 在格式化系统之前,同一段代码一直在工作。 But after the new installations this is not working and I'm getting the error.
但是,在新安装后,此方法无法正常工作,并且出现错误。
Sub Word()
Application.DisplayAlerts = False
Dim ws As Worksheet
Dim Path As String
Dim objWord As Object
Dim objDoc As Object
Path = ActiveWorkbook.Path
Set inv = Workbooks.Open(Path & "\inv.xls")
Set test = Workbooks.Open(Path & "\test.xlsx")
Set ws = inv.Sheets("inv")
Set Wb = test.Sheets("Sheet1")
ws.Range("A1").Copy
Set objWord = CreateObject("Word.Application") <---The error is here
Set objWord.Visible = True
Set objDoc = objWord.Documents.Open(Path & "\test.docx")
objWord.Selection.Paste
Application.CutCopyMode = False
objDoc.Range(0, objDoc.Range.End).Copy
Wb.Range("A1").Select
ActiveSheet.Paste
inv.Close Savechanges:=False
test.Close Savechanges:=True
objWord.ActiveDocument.Close Savechanges:=wdDoNotSaveChange
objWord.Quit
Set objWord = Nothing
Set objDoc = Nothing
End Sub
The line you have indicated for the error seems ok (as long word is normally installed that I'm sure it is the case). 您为该错误指示的行似乎正常(只要安装了长字,我确定是这样)。
What is NOT ok is the successive line: ...Set objWord.Visible = True..
, Certainly this line WILL generate an error cause that is simply a property to set but not an object. ...Set objWord.Visible = True..
行是不好的: ...Set objWord.Visible = True..
,当然,此行将产生错误原因,该错误原因只是要设置的属性,而不是对象。 That should be simply a : objWord.Visible = True
. 那应该只是一个:
objWord.Visible = True
。 Hope this helps. 希望这可以帮助。 KR.
KR。
Please check the below link to track your error. 请检查以下链接以跟踪您的错误。
to properly handle any word instance you could use a helper Function 正确处理任何单词实例,您可以使用辅助函数
Function GetWord(objWord As Object) As Boolean
On Error Resume Next
Set objWord = GetObject(, "Word.Application") ' try getting an already running instance of Word
If objWord Is Nothing Then Set objWord = CreateObject("Word.Application") ' if no running instance of Word then try setting a new one
GetWord = Not objWord Is Nothing ' return the function result
If Not GetWord Then MsgBox "Couldn't get Word", vbCritical ' inform the user that something went wrong
hence your macro could exploit it as follows 因此您的宏可以如下利用它
Sub Word()
Dim objWord As Object
If Not GetWord(objWord) Then Exit Sub
objWord.Visible = True
... rest of your code
End Function
before trying this solution you should close your excel instances and run a new Excel form scratch not to suffer from any residual word instances scattered from your previous attempts 在尝试此解决方案之前,您应该关闭excel实例并运行新的Excel表单草稿,以免遭受先前尝试散布的任何残留单词实例的困扰
should all this not fix the issue, then you have to check more deeply ... 如果所有这些都不能解决问题,那么您必须更深入地检查...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.