Word and Bookmarks question

S

Staunts

Hi there,

I have the following code that I want to print out a series of word
docs and output them with bookmarks inserted. The code runs fine ,
until I put the loop statement in and ask it to open a recordset to
pull out the file names of the documents.

Can any of you have a look and see what I might be doing wrong ?

Using Access 2K/Word 2K

I have the following libraries referenced:

Visual Basic for Applications
Microsoft access 9.0 Object Library
Microsoft DAO 3.6 Object Library
Microsoft Word 9.0 Object Library
Ole Automation

It seems to get a mismatch error on this line:

"Set WordDoc = WordObj.Documents.Open(rst!DocFileName)"

-----CODE STARTS HERE------

Dim rst As DAO.Recordset
Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim WordRange As Word.Range
Dim ProjectName As String
Dim ProjectDate As String

ProjectName = Forms!frmProject!Proj_Title
ProjectDate = Forms!frmProject!Proj_StartDate

Set rst = CurrentDb.OpenRecordset("SELECT DocFilename FROM
tblProjPrintDocs")

Do Until rst.EOF

Set WordObj = CreateObject("Word.Application")
Set WordDoc = WordObj.Documents.Open(rst!DocFileName)

Set WordRange = WordDoc.Goto(What:=wdGoToBookmark,
Name:="ProjectName")
WordRange.InsertAfter ProjectName
'
Set WordRange = WordDoc.Goto(What:=wdGoToBookmark,
Name:="ProjectDate")
WordRange.InsertAfter ProjectDate
WordDoc.PrintOut Background:=False

Set WordObj = Nothing
rst.MoveNext
Loop
WordApp.Quit
rst.Close
------CODE ENDS HERE--------------------

Any help would be greatly appreciated.
 
S

strive4peace

what does the data look like in rst!DocFileName?

does it contain a path as well as a filename?

if not, make sure your default directory is the right place...

WordObj.ChangeFileOpenDirectory "C:\DATA\etc\"


to filter records with no filename filled out...

'~~~~~~~~~~~~~~~~~~~
dim strSQL as string
strSQL = "SELECT DocFilename " _
& " FROM tblProjPrintDocs " _
& " WHERE len(trim(nz(DocFilename))) > 0;"
Set rst = CurrentDb.OpenRecordset(s, dbOpenSnapshot)
'~~~~~~~~~~~~~~~~~~~

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 
S

Staunts

Hi Crystal,

thanks for your help on this.

I've made the changes you suggested but am still getting the error.

Here's what is getting returned with rst!DocFileName:

rst!DocFileName="c:\temp\Doc2.doc"

any sugestions ?

cheers,

Adam
 
S

strive4peace

What is the text of the error message that you get?

Add an error handler to your code

ERROR HANDLER CODE:

put this at the top of your program, right after the procedure
declaration (skip a line first for better readability)

'~~~~~~~~~~~~~~~~~~~~~~
On Error GoTo Proc_Err

'~~~~~~~~~~~~~~~~~~~~~~
... then your statements
'~~~~~~~~~~~~~~~~~~~~~~

put this at the end of the program

'~~~~~~~~~~~~~~~~~~~~~~
Proc_Exit:
On Error Resume Next
'release object variables if any
Exit Function

Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " ProcedureName"

'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit

'~~~~~~~~~~~~~~~~~~~~~~


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 

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