View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Gaetan Gaetan is offline
external usenet poster
 
Posts: 21
Default Microsoft Word Object Library in Excel

I did find code similar to what you've given me on a web site. Wheter it's
that one or the code you've given me, it seems that it doesn't work if I
don't set a reference to the Microsoft Word Object Library. The code results
in an error at the line "Documents.Open (myPath & myFile)".

Would you know why?

....some code before

Set wordApp = CreateObject("Word.Application")
If Err.Number < 0 Then
MsgBox "Please contact your file administrator."
Exit Sub
End If

myPath = "D:\Test\Actions To Complete\"
myFile = Dir(myPath & "*.doc")

Do While myFile < ""
With wordApp
Documents.Open (myPath & myFile)
strDate = ActiveDocument.Tables(1).Rows(1).Cells(2).Range.Te xt
strDate = Left(strDate, Len(strDate) - 2)
End With
Cells(X, 2) = strDate
myFile = Dir
Loop

....some code after


"Jim Rech" wrote:

There are two ways to run Word from Excel: Early binding (by setting a
reference) or late binding (not setting a reference).

When you don't know if Word is not the target machine use late binding
because it allows you to trap the error that occurs if Word is not
installed:

Sub RunWordFromXL()
Dim oWord As Object
Dim oDoc As Object
On Error Resume Next
Set oWord = CreateObject("Word.Application")
If Err.Number < 0 Then
MsgBox "Word not installed or would not start"
Else
oWord.Visible = True
oWord.Activate
Set oDoc = oWord.Documents.Add
oDoc.Range.Text = "Hi"
End If
End Sub

Remember: Do NOT set a reference to Word.

--
Jim
"Gaetan" wrote in message
...
| Hi everyone,
|
| I need to run VBA codes in Excel that uses the Microsoft Word Object
| Library. If the object lbrary is not loaded, the code won't work properly
and
| causes unfortunate problems. Because the code can be run on any given
| machine, there's no way for me to know if the object library has been
loaded
| on that machine.
|
| Is there a way, using VBA, to load this object library before the code
runs,
| or at least to know whether or not the library is loaded so that I can
advise
| the user if it's not?
|
| Thanks for your help.