Move Query result to Word document Bookmark does not work

G

Guest

Hello,
i'm using following SQL stmts to select rows from Table .
When moving the code to the bookmarks in a Word Document I don't see
anything appearing in my word doc.
Word opens the doc all right but then no data appears.
So if the result returns 4 rows in my recordset I want to loop 4 times and
move some of selected fields into the Word-bookmark.
Why does the code not work?

Thanks a lot for your help.

Dim Rsa As Recordset, SQL As String
Dim CurrentDB As Database
Dim Wsa As Workspace
Dim Snap As Recordset
Dim NewQry As QueryDef

SQL = "Select OpdrAnoOmsch, OpdrAnoMainrefNum from OpdrAno where
OpdrAnoMainRefNum='702005004'"
Set Wsa = DBEngine.Workspaces(0)
Set CurrentDB = Wsa.OpenDatabase("OpdrReg.mdb")
Set Rsa = CurrentDB.OpenRecordset(SQL, dbOpenDynaset)
MsgBox "done SQL Select AnoOmsch"
If Rsa.NoMatch Then
MsgBox "NoMatch"
End If

Set oApp = CreateObject("Word.Application")
oApp.Documents.Open strInputFile

Do Until Rsa.EOF

With oApp.Selection
.GoTo Name:="OpdrAnoOmschr"
.Typetext Text:=Rsa("OpdrAnoOmsch")

.GoTo Name:="OpdrMainRefNum"
.Typetext Text:=Rsa("OpdrMainRefNum")
End With
Loop
oApp.Visible = True

Rsa.Close
CurrentDB.Close
MsgBox "done3"
 
V

Van T. Dinh

NoMatch is only used *after* you do a Find... on a Recordset. I am not sure
why you use NoMatch in this case??? You also forgot MoveFirst and MoveNext.

My guess is that you want to make sure it is not an empty Recordset. In
this case, try:

...
Set Rsa = CurrentDB.OpenRecordset(SQL, dbOpenDynaset)
MsgBox "done SQL Select AnoOmsch"
If Rsa.EOF Then
MsgBox "Empty Recordset"

Else
Set oApp = CreateObject("Word.Application")
oApp.Documents.Open strInputFile

Rsa.MoveFirst
Do
With oApp.Selection
.GoTo Name:="OpdrAnoOmschr"
.Typetext Text:=Rsa("OpdrAnoOmsch")
.GoTo Name:="OpdrMainRefNum"
.Typetext Text:=Rsa("OpdrMainRefNum")
End With
Rsa.MoveNext
Loop Until Rsa.EOF
....
End If
 

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