rouxjean
asked on
Best way to export to Word
Hi !
I'd like to know what would be my best bet when trying to export data from a table and/or query to a Word document. Data exported will actually be an invoice so there will be a fixed header and a dynamic content depending of how much items there will be ? Code snippet appreciated !
Thanks
I'd like to know what would be my best bet when trying to export data from a table and/or query to a Word document. Data exported will actually be an invoice so there will be a fixed header and a dynamic content depending of how much items there will be ? Code snippet appreciated !
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
JonoBB: does that mean I can create the demo word file and then insert the field where I want to ? Or do I have to design the Word document on the fly with code ??? (How can I design the word document at first ??? using office97).
Thanks
Thanks
Helen Feddema has done several articles on working with Word and has code samples on here site as well. It is well worth the effort of having a read there :-)
http://www.helenfeddema.com/
Peter
http://www.helenfeddema.com/
Peter
Yeah, you design the demo word document and save it as a templete.....it then fills in the the formfields via the code.......
ASKER
JonoBB: why can't I save it as a doc ? I tried your code but it didn't do nothing...I tried both setting a field and a bookmark, both names FormField1 ....My sql was returning values...All I got was a blank page !
You can save it as a doc if you want...no problems with that, but might cause a sharing violation if a couple of poeple try access the same doc at the same time.
First, make sure that your recordset (and not just the sql) is returning values...if you are 100% sure that the recordset is returnign records, then the problem is with the word doc.
To set up the formfields, get the forms toolbar running (right click on any existing toolbar and select Forms toolbar), then click Text form field on the toolbar.
Once the field is actually inserted in word, double click the field and in the popup box go to the sectin called "Field Settings" and set up the bookmark name there.
You can also set up the format of the text field, i.e. text, date, percent, integer, etc
That should be it....just try it with a single record first until you get it right...then bring in all the data
First, make sure that your recordset (and not just the sql) is returning values...if you are 100% sure that the recordset is returnign records, then the problem is with the word doc.
To set up the formfields, get the forms toolbar running (right click on any existing toolbar and select Forms toolbar), then click Text form field on the toolbar.
Once the field is actually inserted in word, double click the field and in the popup box go to the sectin called "Field Settings" and set up the bookmark name there.
You can also set up the format of the text field, i.e. text, date, percent, integer, etc
That should be it....just try it with a single record first until you get it right...then bring in all the data
ASKER
JonoBB:SELECT * FROM [Quote-Details] returns 6 records...however I got a recordcount of only 1 !!!
Here si the code, i know it's still dirty but i'll clean it up later :
========================== ========== ========== ========== ========== ===
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rsModel As ADODB.Recordset
Dim rsContactDetails As ADODB.Recordset
Dim strSQLModel As String
Dim strSQLContactDetails As String
Dim msgA
Dim strDocName As String
Dim blnContactDetailsID As Boolean
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Const DOC_PATH As String = "C:\access\"
Const DOC_NAME As String = "model2.dot" 'Name of your word document, saved as a Word Template
On Error Resume Next
Set appWord = GetObject(, "Word.application")
If Err = 429 Then
Set appWord = New Word.Application
Err = 0
End If
'Set up the Word variables
Set doc = appWord.Documents(DOC_NAME )
If Err = 0 Then
If MsgBox("Do you want to save the current document " _
& "before updating the data?", vbYesNo) = vbYes Then
appWord.Dialogs(wdDialogFi leSaveAs). Show
End If
doc.Close False
End If
Set dbs = CurrentDb()
strSQL = "SELECT * FROM [Quote-Details]"
Set rs = dbs.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then 'Aucun client trouvé
MsgBox "Customer Not Found"
Else 'Client trouvé
Set doc = appWord.Documents.Open(DOC _PATH & DOC_NAME, , True)
MsgBox rs.RecordCount
With doc
.FormFields("FormField1"). Result = Nz(rs![QuoteNo], 0)
End With
appWord.Visible = True
appWord.Activate
End If
Set rsModel = Nothing
Set rsContactDetails = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
========================== ========== ========== ========== ========== ===
Any ideas, it does import but only one record...
Here si the code, i know it's still dirty but i'll clean it up later :
==========================
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rsModel As ADODB.Recordset
Dim rsContactDetails As ADODB.Recordset
Dim strSQLModel As String
Dim strSQLContactDetails As String
Dim msgA
Dim strDocName As String
Dim blnContactDetailsID As Boolean
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Const DOC_PATH As String = "C:\access\"
Const DOC_NAME As String = "model2.dot" 'Name of your word document, saved as a Word Template
On Error Resume Next
Set appWord = GetObject(, "Word.application")
If Err = 429 Then
Set appWord = New Word.Application
Err = 0
End If
'Set up the Word variables
Set doc = appWord.Documents(DOC_NAME
If Err = 0 Then
If MsgBox("Do you want to save the current document " _
& "before updating the data?", vbYesNo) = vbYes Then
appWord.Dialogs(wdDialogFi
End If
doc.Close False
End If
Set dbs = CurrentDb()
strSQL = "SELECT * FROM [Quote-Details]"
Set rs = dbs.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then 'Aucun client trouvé
MsgBox "Customer Not Found"
Else 'Client trouvé
Set doc = appWord.Documents.Open(DOC
MsgBox rs.RecordCount
With doc
.FormFields("FormField1").
End With
appWord.Visible = True
appWord.Activate
End If
Set rsModel = Nothing
Set rsContactDetails = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
==========================
Any ideas, it does import but only one record...
ASKER
Ok got it fixed by a rs.movelast and rs.movefirst. However it is not dynamic....a bookmark is only good for one record....what do you suggest then ?
ASKER
I need to export my details, which is going to be dynamic...Can be from 0 to 100 records !!!
OK, gotcha,
I think that the formfields method only really supports one record at a time (which we can get around), but in your case, I would then strongly suggest that you set up a mail merge document.
This should get you pointed in the right direction:
http://www.tek-tips.com/faqs.cfm?fid=3237
I think that the formfields method only really supports one record at a time (which we can get around), but in your case, I would then strongly suggest that you set up a mail merge document.
This should get you pointed in the right direction:
http://www.tek-tips.com/faqs.cfm?fid=3237
ASKER
Hi the link you posted makes a copy of the word document for each record ! What I want is the detail to be repeated in the same document, if it fits !!! Any ideas as of how to modify this code ?
I have a sample database of the basic technique that JonoBB described. It's a fully automated interface to word from the Access side using bookmarks in the target document. Drop me a line at jimdettman"at"earthlink.ne
Jim.