I am trying to save Word docs using Excel VBA, but I get the error "ActiveX component can't create object."
When I debug, the error comes from the line: Set wrdApps = CreateObject("Word.Application")
.
It was working fine, but then it just started giving me this error. Does anyone know how to fix this? Thanks for the help!
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
Check that you have the Microsoft Excel Object Library and the Microsoft Office Object Library ticked in Tools > References and that they have been registered.
If they are ticked, you may need to run Detect and Repair from the Excel Help menu to make sure that the Office installation hasn't corrupted in any way.
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.The complete code to use would be:
Is wrdDoc initialised? Are you trying to use wrdDoc before the object has been Set?
Should the first line be ActiveDocument.FullName as in the comments? So:
Try this one.. i've encountered this a lot of time...
so I just run my excel by searching it (located on taskbar), then right click then "run as administrator" or if you already created the excel file, open it from file>open>browse. avoid just double clicking the excel file to open directly.