Hi guys,
I need to put the results of a query to an acces database onto a word document. now the query is driven by user choices so it might return one record or it might return over 300 records or then again, nothing at all. but if there is at least one record returned, this is needed to be desplayed on a word document that is going to have to be designed on the fly in code. since I am new to programming (not a student, working!!) i would like some help on this please. i have got as far as getting the info out of the database, just need help on putting it into a word document.
CarterLangley 0 Newbie Poster
Dani AI
Generated
A concise, practical plan for exporting an Access query to Word from VB6. The code in ’s reply is a useful proof‑of‑concept for launching Word and inserting text; for a reliable tool that must handle 0..300 records the solution should pick one of two structured patterns and follow a few rules.
- Single‑record layout: prepare a Word template (.dot/.dotx) with named bookmarks (or content controls) and fill those placeholders from the Recordset — keeps formatting consistent and makes maintenance easier.
- Multi‑record layout: create a Word table and write directly into table cells (use Range/Cell objects, avoid Selection/Copy‑Paste for performance). The Word object model’s Tables.Add is the right API for creating a table in code. (learn.microsoft.com)
Example (late-binding VB6 pattern — does not duplicate the code posted earlier):
' Assume rs is an open ADODB.Recordset
Dim wdApp As Object, doc As Object, tbl As Object, fld As Object
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set doc = wdApp.Documents.Add
If rs.EOF Then
doc.Content.Text = "No records found."
Else
Set tbl = doc.Tables.Add(doc.Range, 1, rs.Fields.Count) ' header row
Dim c As Long: c = 1
For Each fld In rs.Fields
tbl.Cell(1, c).Range.Text = fld.Name: c = c + 1
Next fld
rs.MoveFirst
Do While Not rs.EOF
tbl.Rows.Add
Dim r As Long: r = tbl.Rows.Count
c = 1
For Each fld In rs.Fields
tbl.Cell(r, c).Range.Text = IIf(IsNull(fld.Value), "", CStr(fld.Value))
c = c + 1
Next fld
rs.MoveNext
Loop
tbl.Columns.AutoFit
End If
doc.SaveAs "C:\Temp\QueryExport.doc"
doc.Close
wdApp.Quit
Set tbl = Nothing: Set doc = Nothing: Set wdApp = Nothing Notes and cautions: don’t depend on RecordCount with a forward‑only cursor — use an EOF loop or open the Recordset with a static/client cursor when a count is required. (learn.microsoft.com) Also, Office automation is unsupported for unattended server‑side use; if this code will run on a server consider alternatives (Open XML, Word Automation Services / server libraries, or exporting an Access report). (support.microsoft.com)
Building the document from a template or filling a programmatically created table covers most needs while keeping the code maintainable and fast.
choudhuryshouvi 33 Posting Pro
Hi guys,
I need to put the results of a query to an acces database onto a word document. now the query is driven by user choices so it might return one record or it might return over 300 records or then again, nothing at all. but if there is at least one record returned, this is needed to be desplayed on a word document that is going to have to be designed on the fly in code. since I am new to programming (not a student, working!!) i would like some help on this please. i have got as far as getting the info out of the database, just need help on putting it into a word document.
I've a code that acts like the similar way u want.Try this code.I think it will give u an idea how to do word writing from vb programs.If u still have problems then mail me.My email id is :
Insert the following references into your project before trying to run this code :
Microsoft Word <version no> Object Library
from Project->References
Here is the code:-
'add a textbox(text1).also add two command buttons and name 'them cmdclose & cmdword respectively.
Option Explicit
Dim wrdapp As Word.Application
Private Sub cmdclose_Click()
On Error GoTo cerror
wrdapp.ActiveDocument.Close
wrdapp.Quit
cerror:
If Err.Number = 4198 Then
Dim ex As Integer
ex = MsgBox("Do you want to save this file or quit ?", vbYesNo + vbQuestion, "Microsoft Word with VB")
If ex = vbNo Then
'Close the current document
wrdapp.ActiveDocument.Close
'Close Word
wrdapp.Quit
ElseIf ex = vbYes Then
Exit Sub
End If
'Exit Sub
End If
End Sub
Private Sub cmdword_Click()
Set wrdapp = New Word.Application
With wrdapp
'Show Word
.Visible = True
'Create New Document
.Documents.Add
'Add text to the document
.ActiveDocument.Content.Text = "Hi, " & Trim(Text1.Text)
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set wrdapp = Nothing
End Sub
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.