Reading word properties in access 2003

F

flemming johansen

Hi.

I'm trying to read word document properties in access 2003. But I get the
following error:
Run-time error
Automation error
the module was not found

I'm using the following code:
Dim wd As New Word.Application
Dim doc As Word.Document
Dim sFileName As String
'
' read doc path from table: Setup
'
Set rstSetup = db.OpenRecordset("Setup", dbOpenDynaset)
With rstSetup
If rstSetup.BOF Then
MsgBox ("Sti til doc ikke defineret. ")
rstSetup.Close
GoTo lut
End If
rstSetup.MoveFirst
docpath = !PathDoc
End With
rstSetup.Close


sFileName = Dir(docpath & "\*.doc") 'Gets the first Word document

Do While sFileName <> ""
Set doc = wd.Documents.Open(docpath & "\" & sFileName)
.... more code here

the above "Set doc" statement generates the error.

Can someone help me.
 
J

Jim Burke in Novi

Also, make sure you close Word when you're done:

wd.Quit
Set wd = Nothing
 
F

flemming johansen

Hi Jim

I have tried to add the "Set wd = New Word.Application" but I stil get the
same error.

I have included the following reference
microsoft word 11.0 object library

Do I need a specifik reference in order to read word properties.
 
J

Jim Burke in Novi

My experience with Word is pretty limited. In my appl, I use the following
when printing a Word doc (objWord is my Word appl object):

objWord.Documents.Add fileName
objWord.ActiveDocument.PrintOut , , , , , , , copies
objWord.ActiveDocument.Close

I never open an existing doc and reference the fields, but I would think the
same logic would work (do the Add, then reference the active doc fields). I
create a new doc, then set the fields this way:

objWord.Documents.Add dictationServerDir & dictationTemplateName
With objWord.ActiveDocument.Bookmarks
.Item("PatientName").Range.Text = PatientName

I've never used the Open method, so I'm not sure how that works exactly.
 
B

BeWyched

Hi

Try :

Dim wd, doc
Set wd = CreateObject("Word.Application")
Set doc = wd.Documents.Open(docpath & "\" & sFileName)

You will also need:
wd.Visible = True

I use this extensively with Word/Access 2003 and it works.

Cheers.

BW
 
F

flemming johansen

Hi.

I have now tried both methods without any luck. See error when debugging
below.
If it works for you then the code is ok, but I may have a problem with the
resources I have attached to access in the tool menu):
'methode 1
Dim wd As New Word.Application
Dim doc As Word.Document

'methode2
Dim wd2
Dim doc2

'-------------

Set wd = New Word.Application
Set wd2 = CreateObject("Word.Application")
wd.Visible = True
wd2.Visible = True

Set doc = wd.Documents.Open(docpath & "\" & sFileName)
Set doc2 = wd2.Documents.Open(docpath & "\" & sFileName)

'error when debugging
wd = <Object variable or with block variable not set>
wd2 = Nothing
 
J

Jim Burke in Novi

Not sure what to tell you. If you have the Word reference set, my way of
doing it should work, and for Bewyched's method you don't even need the
reference since it uses late binding. FYI, you only need to set Word's
visible property to true if you need to have Word opened as a visible
application - if you just want to reference fields from a document, there's
no need to set visible = true. There's got to be some reason you're geting
that error, but I can't figure it out. It works fine for me in my appl with
the exact code I gave you and nothing more. Are you absolutely certain that
you have the correct file names? That's the only thing I can think of that
could be screwing things up. I'd check the value you have for the file name
in debug mode right before you execute the

Set doc = wd.Documents.Open(docpath & "\" & sFileName)

statement. Maybe the filename is bad and that causes the error? You're not
getting an error when you set your new instance of Word, so that part must be
working. And if Access is allowing the Open method of Word (i.e. it compiles
successfully when you have wd declared as Word.Application), the only thing I
can think of is that your filename isn't valid.
 
B

BeWyched

Hi. Like Jim, this is baffling me.

Try,

Dim wd
Set wd = CreateObject("Word.Application")
wd.Visible = True

This will prove whether Word can be opened without the potential problem of
an incorrect file path. You should see Word withouty any loaded document.

If you can't, you could try copying the above into, say, an Excel macro,
running it, and see if it works. This should prove whether you have a problem
with Word, or with Access. If Access, then are you sure you have the correct
references set (to MS Word library files) and does the code compile?

I have experienced a similar problem with Access 2003 trying to automate
Word 2007. The problem was that Word was unstable and this was fixed by
running MS Office Repair.

Cheers.

BW
 
J

Jim Burke in Novi

Did you check your value for the filename in debug mode to make sure it's
valid? Don't just assume it is because the code looks right. I'd definitely
do that right away if you haven't already, because I can't see any other
reason for it not working. If you know for a fact that your code that
generates the file name is working right, then I'd re-post the question,
maybe post it on the Word Programming forum. I'm not an expert like some of
the folks here, and maybe one of the MVPs will spot something or have another
suggestion. There HAS to be some explanation.
 

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