简体   繁体   中英

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.

But getting the

runtime error 429 Activex component can't create object.

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. That should be simply a : objWord.Visible = True . Hope this helps. KR.

Please check the below link to track your error.

Microsoft Help for Automation 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

should all this not fix the issue, then you have to check more deeply ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM