0
votes

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
3
Is Word actually installed on the machine and correctly registered? Beyond that a number of things are not good in the code you show us. Besides Walters catch: You shouldn't name the procedure Word as that could be misunderstood by VBA. You don't declare inv, wb or test - do you have Option Explicit at the top of the code module? If not, add it to prevent problems caused by spelling errors and similar. - Cindy Meister
And also objWord.ActiveDocument.Close Savechanges:=wdDoNotSaveChange - here you should be using the object you declared for the Word document, objDoc, not ActiveDocument. - Cindy Meister
Cindy, your observations are valuable. But the same piece of code was working before I formatted my system. But after the new installations this is not working nad getting the error. - Rishi
Then I come back to my very first observation: Is Word correctly installed and registered - apparently not. Try to repair Word/Office. Also, you say new installation: what is different from before? - Cindy Meister
System was formatted and every application was freshly installed. - Rishi

3 Answers

0
votes

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.

0
votes

Please check the below link to track your error.

Microsoft Help for Automation Error

0
votes

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 ...