OLE Question

R

Ron

I'm using OLE to transfer data from Access to Word, the
problem I'm having is closing the Word documents, the
user gets a Close prompt for each document created(which
I'm trying to avoid). Can you tell me how to close the
documents in the code? Here is a code sample:

Dim db As Database, cSQL As String, rs As Recordset
Dim objWord As Word.Application

cSQL = "SELECT * FROM tblRespondent ORDER BY Number, Name"
Set db = CurrentDb()
Set rs = db.OpenRecordset(cSQL)

If rs.RecordCount > 0 Then
rs.MoveFirst
While Not rs.EOF
'Launch Word and load template
Set objWord = New Word.Application
objWord.Documents.Add _
Application.CurrentProject.Path & "\CHRONOI.dot"
objWord.Visible = False
With objWord.ActiveDocument.Bookmarks
.Item("Number").Range.Text = rs!Number
.Item("Respondent").Range.Text = rs!Name
objWord.PrintOut
End With
rs.MoveNext
Wend

End If

rs.Close
Set rs = Nothing
 
C

Chad DeMeyer

First, I recommend you use a second object variable for the document you are
creating:

Set objDoc = objWord.Documents.Add Application.CurrentProject.Path &
"\CHRONOI.dot"

I assume you want to save these documents, but just don't want the user to
be prompted. In that case:

objDoc.SaveAs 'This method takes several arguments. For detailed
guidance see the Word VBA help files.

Or, if you don't need to save the documents:

objDoc.Saved = True

should suppress the prompt if no other changes are made before closing the
document.

Regards,
Chad DeMeyer
 
G

Guest

Thanks, for the advice. I appreciate it....Ron
-----Original Message-----
First, I recommend you use a second object variable for the document you are
creating:

Set objDoc = objWord.Documents.Add
Application.CurrentProject.Path &
 

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