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?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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...
 
Upvote 0
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
 
Upvote 0
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).
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,156
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top