Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Word reference not function | Automation error - Library not registered | Error -2147319779 (8002801d)

Tags:

ms-word

excel

vba

Context

I'm using Word from Excel to open documents and update links in them.

At first, Word was opening and opened the Documents, but I got the error on the line :

For Each aField In oDoc.Fields

After trying to simply uncheck, then re-check the "Microsoft Word" reference,
I also had the same error on :
Set oW = VBA.GetObject(, "Word.Application") and
Set oW = VBA.CreateObject("Word.Application")

And yet the declarations of variables like oW As Word.Application don't seem to cause any issue...

Dim oW As Word.Application, _
    oDoc As Word.Document, _
    aField As Word.Field, _
    aIs As Word.InlineShape, _
    aSh As Word.Shape, _
    fCt As Integer, _
    isCt As Integer, _
    i As Integer, _
    NewFile As String, _
    TotalType As String

On Error Resume Next
Set oW = VBA.GetObject(, "Word.Application") '<-- Error after trying
On Error GoTo 0
If oW Is Nothing Then Set oW = VBA.CreateObject("Word.Application") '<-- Error after trying

NewFile = Dir(FolderToScan & "*." & FileExtensionWOpoint)

Do While NewFile <> vbNullString
    Set oDoc = oW.Documents.Open(FolderToScan & NewFile)
    fCt = oDoc.Fields.Count
    isCt = oDoc.InlineShapes.Count

    For Each aField In oDoc.Fields    '<-- 1st error

Err.Number = -2147319779
Err.HelpContext = 1000440
Err.Description = Automation error - Library not registered.
Err.Source = VBAProject
Err.HelpFile = C:\PROGRA~2\COMMON~1\MICROS~1\VBA\VBA7.1\1036\VbLR6.chm
Err.LastDllError = 0


Tests to solve issue

Test 1 : Manually add the reference to the library

I wanted to try the following:

  1. In VB project, see where the file for the reference "Microsoft Word" is
    (for me, C:\Program Files (x86)\Common Files\microsoft shared\OFFICE15\MSWORD.OLB)
  2. Remove the reference to the "Microsoft Word"
  3. Close and save the project
  4. Go to the folder of the file's reference and run regsvr32 MSWORD.OLB in the console
  5. Reopen the project, re-add the reference to the "Microsoft Word"

But in the mean time (between 1. and 4.), the file's reference MSWORD.OLB has disappeared!!!! o_O

I've reboot and checked and my hidden files are visibles, so I just don't know what to do!!!
Is there another way to restore the library? Or to force loading it?

I rebooted again and this time the Path of the reference changed to C:\Program Files (x86)\Microsoft Office\Office15\MSWORD.OLB, so the reference seems to works but I've got the same error on the same lines.

Apparently regsvr32 onlt works for DLL and OCX, so the I couldn't manually add the reference.

Test 2 : Repair Office installation

I've repaired Office installation, but same issue on same lines...

Test 3 : Start from cmd and add to PATH environment variable

As @S Meaden suggested, I tried opening Word from command lines :

In a random path :
winword.exe /automation, winword.exe, winword /automation and winword say 'winword.exe.' is not recognised
But start winword and start winword /automation work smoothly.

I've tried "C:\Program Files (x86)\Microsoft Office\Office15\winword.exe" /r to force registration but it didn't change a thing...

And in the right path:
All the commands run smoothly!

I've then added the path C:\Program Files (x86)\Microsoft Office\Office15\ into PATH environment variable:
Afterwards, all commands work, but I still get the same error in VBA...

Test 4 : Windows Registry and other versions of Office

I only have one version of MS Office on my computer and it wasn't upgrading or anything.

I still check in the Windows Registry to be sure, but there is no reference to any other version!

I just found the Policy value that were referenced for version 14 (I have 15, Office 2013), so I backed it up and delete it. But no changes, so I re-add it!

Test 5 : Force the registration from cmd

I've tried to force the registration of both Excel and Word with :

"C:\Program Files (x86)\Microsoft Office\Office15\winword.exe" /r
"C:\Program Files (x86)\Microsoft Office\Office15\winword.exe" /regserver
"C:\Program Files (x86)\Microsoft Office\Office15\excel.exe" /r
"C:\Program Files (x86)\Microsoft Office\Office15\excel.exe" /regserver

but it didn't change a thing...

Test 6

Still looking for thins to try...


Question

The reference is pointing an existing file again,
Word is still fully functional
but my code that was working yesterday is now erroring on :

Set oW = VBA.GetObject(, "Word.Application")
Set oW = VBA.CreateObject("Word.Application")

Err.Number = -2147319779
Err.HelpContext = 1000440
Err.Description = Automation error - Library not registered.
Err.Source = VBAProject
Err.HelpFile = C:\PROGRA~2\COMMON~1\MICROS~1\VBA\VBA7.1\1036\VbLR6.chm
Err.LastDllError = 0

Does anyone know a way around?


Simple WorkAround (not fixed)

Just using Late Binding seems to do the trick for me, so even if I'm curious about why, I'm just glad this work again!

So now my declarations look like this :

Dim oW As Object, _
    oDoc As Object, _
    aField As Object, _
    aIs As Object, _
    aSh As Object, _
    fCt As Integer, _
    isCt As Integer, _
    i As Integer, _
    NewFile As String, _
    TotalType As String
'' Instead of :
'Dim oW As Word.Application, _
    oDoc As Word.Document, _
    aField As Word.Field, _
    aIs As Word.InlineShape, _
    aSh As Word.Shape, _
    fCt As Integer, _
    isCt As Integer, _
    i As Integer, _
    NewFile As String, _
    TotalType As String
like image 467
R3uK Avatar asked Jan 22 '26 05:01

R3uK


1 Answers

Ok, let me formalise some advice given in comments. So we need to check your COM plumbing. When you create a COM object with VBA.CreateObject("Word.Application") there is a sequence of events centred on the Registry and you should double check using RegEdit.

N.B. I am running Windows 8.1 and my Word is version 15, yours maybe different.

Firstly the string is looked up in the under the hive HKEY_CLASSES_ROOT, and I can find mine here is a registry snippet demonstrating

[HKEY_CLASSES_ROOT\Word.Application]
@="Microsoft Word Application"

[HKEY_CLASSES_ROOT\Word.Application\CLSID]
@="{000209FF-0000-0000-C000-000000000046}"

[HKEY_CLASSES_ROOT\Word.Application\CurVer]
@="Word.Application.15"

[So actually we can this indicates VBA.CreateObject("Word.Application") and VBA.CreateObject("Word.Application.15") should resolve to the same.] From this we have the CLSID for the Word Application , it is {000209FF-0000-0000-C000-000000000046}. So I next go looking for that key.

I search for the CLSID firstly under HKEY_CLASSES_ROOT\CLSID where in the 32-bit world all classes would be found but it is not there because it is, for me, a 64 bit alternative key, HKEY_CLASSES_ROOT\Wow6432Node\CLSID\

You should find the CLSID either under the 32-bit key or the 64-bit key. Under this key you should find a number of subkeys the vital one is LocalServer32 and the path to the server executable (which for Word is the same as the user application executable) is found there. For me this is

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{000209FF-0000-0000-C000-000000000046}\LocalServer32]
@="C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\WINWORD.EXE /Automation"

So now from a command line I can run to test the launching of Word using COM.

C:\Program Files\Microsoft Office 15\Root\Office15\WINWORD.EXE /Automation

You should trace the same chain of invocation and resolve any problems. Please give feedback.

If that works then check your variable declaration. Experiment and see if there is a change of behaviour/error if one case used Early-binding and the other uses late binding.

UPDATE: I googled on your error number but your get much better results if you use it hex equivalent 8002801D, oplenty of links to look through there,

Google error 8002801D

Ok, from your comment feedback, glad that the late binding works, this proves that aspect of the COM plumbing and it places the blame on the type library as intuitable from the error message. I'm glad we have same version so we can share another snippet of the registry. So the type libraries that appear in the Tools->References dialog in VBA IDE is based on the details under HKEY_CLASSES_ROOT\TypeLib for the type library we're both using my registry says the following

[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}]

[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.6]
@="Microsoft Word 15.0 Object Library"
"PrimaryInteropAssemblyName"="Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"

[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.6\0]

[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.6\0\Win32]
@="C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\MSWORD.OLB"

[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.6\Flags]
@="0"

[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.6\HelpDir]
@="C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\"

So we differ as to the location of MSWORD.OLB mine is at (as is another commenters, Vincent G)

C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\MSWORD.OLB

So perhaps this is at the root of your problem.

like image 86
S Meaden Avatar answered Jan 23 '26 19:01

S Meaden