View Full Version : Problem with filling in Bookmarks in Word


gaucho_belgic
11-04-2005, 05:58 AM
Hi all,

I'm experiencing some problems when filling in bookmarks in word.

With my first query (single row returned), no problem at all. Yet, with my new query (which now return 2 rows), i get some error message. --> "Too few parameters, expected 1).

I'm having a table which describes what bookmark fits to what field. I query this table to generically fill out my word-document.

I pasted my code below, you can still see my first query (in comment). It's the 'dbSql' query.

Probable i'll have to put in some intelligence to figure out it is a more-than-1-row-returned query?

Grts,
Kevin

************************************
** CODE**
************************************
Option Compare Database
Option Explicit

Sub ReportToWord(filename, newfilename)
On Error GoTo ReportToWord_err

Dim objWord As Object
Set objWord = Nothing
Set objWord = CreateObject("Word.application")
objWord.Documents.Open filename
objWord.Visible = False

Dim db As Database
Dim docRecSet As Recordset
Dim docSql As String
Dim dbRecSet As Recordset
Dim dbSql As String

Dim Pr_id As Integer

Pr_id = [Forms]![_MsWord]![cmbProject]

Set db = CurrentDb

docSql = "select * from _PCPD"
Set docRecSet = db.OpenRecordset(docSql, dbOpenSnapshot)

dbSql = "select tblProject.nickname,tblProject.internalreleasedate ,tblProjectRun.ProjectRunType from tblProject,tblProjectRun where tblProject.Project_id=tblProjectRun.project_key and tblProject.Project_ID= " & Pr_id
'dbSql = "select tblProject.nickname,tblProject.internalreleasedate from tblProject where tblProject.Project_ID= " & Pr_id
Set dbRecSet = db.OpenRecordset(dbSql, dbOpenSnapshot)

With docRecSet
Do Until .EOF
With objWord.activedocument.bookmarks
objWord.activedocument.bookmarks(docRecSet.Fields( 1)).select
objWord.selection.Text = dbRecSet.Fields(docRecSet.Fields(0))
End With
.MoveNext
Loop
.Close
End With

objWord.activedocument.SaveAs newfilename
objWord.activedocument.Close
objWord.Quit
Set objWord = Nothing

ReportToWord_exit:
Exit Sub

ReportToWord_err:
MsgBox (Err.Description)
objWord.activedocument.Close
objWord.Quit
Set objWord = Nothing
GoTo ReportToWord_exit


End Sub

Private Sub cmdCreatePCPD_Click()
Dim filename As String
Dim newfilename As String

filename = "L:\RPC\PRODUCT_DEVELOPMENT\Products\ProductDb\Prod uctCreation_template.doc"
newfilename = "L:\RPC\PRODUCT_DEVELOPMENT\Products\ProductDb\Prod uctCreation_RunXXX.doc"

Call ReportToWord(filename, newfilename)
End Sub
********************************