Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Reports to WORD - URGENT 2

Status
Not open for further replies.

leslie746

MIS
May 31, 2004
28
GB
I have a report based on a query resulting in over 100 records. I want to save each record as a Word document based on a template.
My problem is that when I use VB code with reference to recordset, it revolves around first record only and I would like to be able to save all individual records into individual Word document.Currently it saves the first one only.
Deadline by tomorrow morning.
Any suggestions, please?
 
And what have you so far ?
Why not use the mailmerge facility of word ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
MailMerge would not save the individual documents, it would be good only for printing out, which is not exactly the problem I have. I need to be able to save them.
Thank you for your prompt reply.
 
Try something like this to input records into bookmarks established in an existing MS Word template:
Code:
[COLOR=green]'Open Word, import the text to the bookmarks and display the letter[/color]
    Set objWord = New Word.Application
    objWord.Visible = True
    Set objWordDoc = objWord.Documents.Add(Template:="PathToTemplate\TemplateName.dot", NewTemplate:=False)
[COLOR=green]'Open Your Recordset and move to the first record[/color]
Set DB = CurrentDb
Set Rs = Db.OpenRecordset (“YourRecordset”, DbOpenDynaset)
Rs.MoveFirst
[COLOR=green]'Loop through each record of the Rs, inserting each field of the record into a Bookmark in the template[/color]
Do Until Rs.EOF
    With objWordDoc.Bookmarks
        .Item("Bookmark1").Range.Text = Rs.Fields(1)
        .Item("Bookmark2").Range.Text = Rs.Fields(2)
         etc
    End With

    
[COLOR=green]'Save the letter to the desired folder[/color]
    objWord.ActiveDocument.SaveAs FileName:="YourDocName.doc"
        objWord.Quit
Rs.MoveNext
Loop

NOTE: This code is for DAO

HTH
Lightning
 
Thank you very much for your kind advice.
I have followed it to the letter,however, when the script reaches the end of the first loop, it breaks with the following message: "Run-time error -2147417848(80010108) Automation error
The object invoked has disconnected from its clients."
When I open Debug, it shows the line:
"With objWordDoc.Bookmarks" - highlighted.
Can you assist, please?
 
Sorry, My fault entirely.

The line:
Code:
        objWord.Quit
needs to be outside the loop, not inside as I posted. Move that line so that it is the line following the Loop statement and you should be fine.

The other thing this code snippet does not do is uniquely name the Word doc when it saves the doc. If you want separate docs for each record you would need to amend the code to do this as well.

HTH
Lightning
 
Thank you again.
Interestingly enough, after following this latest correction, it creates only one document into which fields of all rows are inserted into the bookmarks, so I am ending up with bookmarks filled with say 3 values (For testing purpose I am using dateset containing only 3 rows).
Any thoughts about this?
By the way, I have line:
"objWord.ActiveDocument.SaveAs fullname" where fullname is derived from value of one field concatenated with current date, something like :
fname = Left$(Organisation, 8) & "_" & Datex & ".doc" where Datex is a variable holding the Date formatted to look like '010604'. This part works OK and the resulting file is saved under the required filename, but only once contains all three rows as mentioned above.
 
Leslie
Try adding a line to close the template inside the loop, using the NoSave option. Then move the Open Template code inside the loop.

I'll have a think about it and post back in the morning (it's just after midnight here, and it's been a loooong day).

Lightning
 
Lightning (is it your name?)
Thank you for your kind assistance, have a nice and quiet good night.Hope to hear from you at your earliest convenience. Best Regards.
Leslie
 
Try this amended code:
'Open Word, import the text to the bookmarks and display the letter
Set objWord = New Word.Application
objWord.Visible = True
'Open Your Recordset and move to the first record
Set DB = CurrentDb
Set Rs = Db.OpenRecordset (“YourRecordset”, DbOpenDynaset)
Rs.MoveFirst
'Loop through each record of the Rs, inserting each field of the record into a Bookmark in the template
Do Until Rs.EOF
Set objWordDoc = objWord.Documents.Add(Template:="PathToTemplate\TemplateName.dot", NewTemplate:=False)
With objWordDoc.Bookmarks
.Item("Bookmark1").Range.Text = Rs.Fields(1)
.Item("Bookmark2").Range.Text = Rs.Fields(2)
etc
End With
'Save the letter to the desired folder
objWordDoc.SaveAs FileName:="YourDocName.doc"
objWordDoc.Close
Set objWordDoc = Nothing
Rs.MoveNext
Loop
objWord.Quit
Set objWord = Nothing

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

Thank you for your help so far.
Can anybody advise me how to incorporate the Progress Bar to show the progress of the comand:

objWordDoc.SaveAs FileName:="YourDocName.doc" as in above coding.

Early reply shall be most appreciated.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top