open word from excel...file does not exist error

G

Guest

I am trying to open a word dopcument from excel...below is the code I am
running but I receive the following error....that is runtime error
524...which indicates that the file does not exit...but it does. I searched
through the other questions and did make sure that the microsoft Word 11.0
object library is available. What am I still doing wrong.

Sub OpenAWordDoc()
Dim WordApp As Object, WorddDoc As Object

Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Open "C:\Documents and Settings\files\Test.doc"
WorddApp.Visible = True

End Sub
 
G

Guest

Hi, im using this and it's working. Don't use CreateObject but GetObject like
this:

Dim Word As Object
Dim MyXL As Object

Sub Open_Wordfile()

Set Word = GetObject("C:\Documents and Settings\files\Test.doc")
Set MyXL = GetObject(, "Word.application")
MyXL.Application.Visible = True

End Sub

„tbaam" napísal (napísala):
 
G

Guest

First, thank you for trying to help me...

I tried exactly what you have here and now I get

Run-Time error 432
Filename or class name not found during automation operation.

Any other suggestions would be greatly appreciated.
 
G

Guest

Hi, I have tryid my code also in Office 2003 and it was working properly
(with other path and other file then before). Check your path and the file
name.

Henrich

„tbaam" napísal (napísala):
 
D

Dave Peterson

Are you sure it's 524 and not 424?

If the error is really 424, it could be because you misspelled WordApp in the
..visible line.

(I don't get 524 when the file doesn't exist (Office 2003).)
 
G

Guest

So I changed the code to this
Sub Open_Wordfile()
Dim Word As Object
Dim MyXL As Object
Set Word = GetObject("C:\Documents and Settings\files\test.doc")
Set MyXL = GetObject(, "Word.application")
MyXL.Application.Visible = True

End Sub

The file does exist but I now receive the following error:
Run-time error 432 "File name or class name not found during automation
operation"

....looked for info on this and found that I need this dll.
regsvr32.exe C:\WINDOWS\SYSTEM\Dao360.dll

I installed it and it said it was correct....

but i still get the error...any ideas
 
D

Dave Peterson

I saved this from a previous post. Maybe it'll help you:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPWD As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\doc10.doc"
myPWD = "mypassword"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

WDDoc.WritePassword = myPWD
WDDoc.Close savechanges:=True

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

The purpose of this code was to open a file and give it a password. But there
might be something that you can use.
 
G

Guest

PERFECT!!!!!!!

Thank You

Dave Peterson said:
I saved this from a previous post. Maybe it'll help you:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPWD As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\doc10.doc"
myPWD = "mypassword"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

WDDoc.WritePassword = myPWD
WDDoc.Close savechanges:=True

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

The purpose of this code was to open a file and give it a password. But there
might be something that you can use.
 
J

Jana

Curt:
You might need a reference in the VB to Microsoft Word # Object
Library. (Mine is 11.0, your # might be different) I had to create
that reference before it would work for me.

HTH,
Jana
 
D

Dave Peterson

The first declarations (As Word.Application) expect to have a referent to
microsoft word.

Open that workbook
go into the VBE
select that project
tools|References
Look for Microsoft Word and check it.

Alternatively, you could just use the "as object" lines and it should work ok.

copied code and ran first line Dim WDApp As Word.Application gave error
user defined type not defined Went back and noticed ' in front of Dim put it
back in then 2nd line gave same error. Went back and removed '. I am stumped
as to why this machine of mine will find the file on A but so much problem on
C. I read something about a reference to something by someone any ideas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top