Unable to open a Word document in Excel VBA

sts023

Board Regular
Joined
Sep 1, 2008
Messages
106
Hi all....

All I'm trying to do is to "read" a Word 2010 document in Excel 2010 using Excel VBA.

I've tried various different ways of coding this
This one -
VBA Code:
Option Explicit
Sub Main()
Dim oWDoc As Word.Document
Dim oWord As Word.Application
Dim strPath As String
Dim oWPara As Word.Paragraph

  strPath = "I:\test.doc"
  Set oWord = Word.Application

Set oWDoc = oWord.Documents.Open(strPath)
For Each oWPara In oWDoc.Paragraphs()
'Do something
Next oWPara
Cleanup:
oWDoc.Close
  Set oWDoc = Nothing
  oWord.Quit
  Set oWord = Nothing
End Sub
generates
Run-time error '-2147221164 (80040154)':
Class not registered.

Prior to this I tried
VBA Code:
Sub Copy_From_Word()
Dim objDoc As Object
Dim objPara As Object
Dim objWord As Object
Dim strPath As String
'*
'** Open a Word Document and Set it to the
'** newly created object above
'*
strPath = "I:\test.doc"
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(strPath)
'*
'** Store all the content of that Word
'** Document in a variable.
'*
For Each objPara In objDoc
' If objPara.Type = wdParagraph Then
' Do something
  Next objPara
  objDoc.Close SaveChanges:=wdDoNotSaveChanges
objWord.Quit

Set objDoc = Nothing
Set objWord = Nothing
End Sub
This gets:-
Run-time error '-2147319779 (8002801d)':
Automation error
Library not registered

I have Tools/References to
Visual Basic for Applications
Microsoft Excel 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Scripting Runtime
Microsoft Word 14.0 Object Library

Incidentally, after a lot of Googling, I found and executed Microsoft's
MicrosoftEasyFix25011.mini.diagcab

This seemed to "reset" some long-standing Excel options, but had no effect on the errors.

Can any kind soul offer a solution, help or advice?
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,573
HTH. Dave
Code:
Dim oPara As Object
oWord.Documents.Open Filename:="I:\test.doc"
'loop paras
For Each oPara In oWord.ActiveDocument.Paragraphs
'if not blank para
If oPara.Range.Text <> Chr(13) Then
 

sts023

Board Regular
Joined
Sep 1, 2008
Messages
106
Sorry Dave - same set of problems
Full (amended) code
VBA Code:
Sub DavesReply()
Dim oPara As Object
Dim oWord           As Word.Application
  oWord.Documents.Open Filename:="I:\test.doc"
'loop paras
For Each oPara In oWord.ActiveDocument.Paragraphs
'if not blank para
If oPara.Range.Text <> Chr(13) Then
End If
Next oPara
End Sub
This gets ""Run-time error 91 - Object Variable or With block variable not set" on the Open statement.

When I try variations like
VBA Code:
Dim oWord As Object
  Set oWord = Word.Application
I'm back to originalproblem(s).

Any more ideas?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,871
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Dim oPara As Object, oWord As Object, docWord As Word.Document
Set oWord = New Word.Application
Set docWord = oWord.Documents.Open("I:\test.doc")
oWord.Visible = True
For Each oPara In docWord.Paragraphs
etc
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,573

ADVERTISEMENT

This should work. Dave
Code:
Sub DavesReply2()
Dim oPara As Object
Dim oWord As Object
On Error Resume Next
Set oWord = GetObject(, "word.application")
If Err.Number <> 0 Then
On Error GoTo 0
Set oWord = CreateObject("Word.Application")
End If
oWord.Documents.Open Filename:="I:\test.doc"
'loop paras
For Each oPara In oWord.ActiveDocument.Paragraphs
'if not blank para
If oPara.Range.Text <> Chr(13) Then
'do something if not blank para
End If
Next oPara
Set oPara = Nothing
oWord.ActiveDocument.Close SaveChanges:=False
oWord.Quit
Set oWord = Nothing
End Sub
 

sts023

Board Regular
Joined
Sep 1, 2008
Messages
106
Yongle - Same set of problems as before, but thanks for trying!

Dave is todays prize winner - it works!!!

Stepping through Dave's revised code, "GetObject" doesn't raise an error.
Is there a simple explanation as to what the difference is between "GetObject" or "CreateObject", and ""Set objWord" that a dimwit like me could comprehend?
The Get and Create seem to reference ActiveX objects; (I vaguely understand the concept), but "Set" seems to be more straightforward - a sort of "set this generic Object to a specific type". Pity it doesn't seem to work!

Anyway, thanks to all who considered helping - much appreciated...
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,573

ADVERTISEMENT

The difference between the codes is that Yongle's code uses early binding which requires that the Word object library be referenced within the VBE. My code uses late binding which does not require the reference to be set. The Get Object doesn't raise an error if the Word application is not running but instead creates it. If the Word application is running then Get Object does cause an error which is the reason for the On Error Resume Next statement preceding it. If the Word application is running it then sets the oWord object to that existing Word application. It then goes on to reset the error control with On Error goto 0. Thanks for posting your outcome. Dave
 

sts023

Board Regular
Joined
Sep 1, 2008
Messages
106
Thanks Dave, but I still don't understand why my original code failed on
VBA Code:
Set oWord as Word.Application
and its variations.

Presumably "Set xxx" works quite differently to "GetObject" or "CreateObject.

If you've the time (and patience!) to reply I'd be happy to try to understand the difference, but if you've run out of either commodity I'd completely understand (and still be grateful).
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,573
If you set a reference to the Word library and use early binding then you just need to Dim the variable to use it. HTH. Dave
Code:
Dim oWord as Word.Application
 

Watch MrExcel Video

Forum statistics

Threads
1,113,979
Messages
5,545,316
Members
410,676
Latest member
M0J0jojo
Top