Mpikkis


Hi all, i am new here, and i have no idea how to do this. I am trying to do like a form, fill in the fields necessary and then with a press of a button to take all the information and place them in a word document template. I am a bit familiar with access. but i donno anything about the rest. If there is a way to do this, i will need an example to understand it.

Thanks in advance



Re: Put access fields in specific word document places

RhysDavies


Hi - i was looking to do the same thing and found an excellent article that makes it really easy. Create your word document and then create bookmarks where you want access to populate your word document with data. In word , go to insert, bookmark and ideally give the bookmark the same name as the access field you want to use. create as many bookmarks as required, i have ommitted a lot of mine from this reply to make it easier to see. then in your access form create a command button, mine is called createpo because its for creating purchase orders and in that command button enter the following code:

Private Sub createpo_Click()

On Error GoTo MergeButton_Err
Dim objWord As Word.Application

' Start Microsoft Word.
Set objWord = CreateObject("Word.Application")
With objWord
' Make the application visible.
.Visible = True
' Open the document.
.Documents.open ("Z:\purchaseorder.doc") 'this is the name and location of the word file that you want to open
' Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("companyname").Select 'this should be the name of the bookmark you created in word
.Selection.Text = (CStr(Forms!frmpo!companyname)) 'this should be the reference to the field in the form
.ActiveDocument.Bookmarks("daddress1").Select
.Selection.Text = (CStr(Forms!frmpo!daddress1))
.ActiveDocument.Bookmarks("daddress2").Select
.Selection.Text = (CStr(Forms!frmpo!daddress2))
.ActiveDocument.Bookmarks("dtown").Select
.Selection.Text = (CStr(Forms!frmpo!dtown))
.ActiveDocument.Bookmarks("dcounty").Select
.Selection.Text = (CStr(Forms!frmpo!dcounty))
.ActiveDocument.Bookmarks("dpostcode").Select
.Selection.Text = (CStr(Forms!frmpo!dpostcode))
.Selection.Text = Format(CDbl(Forms!frmpo!price), "#,##0.00")
.ActiveDocument.Bookmarks("vat").Select
.Selection.Text = Format(CDbl(Forms!frmpo!VAT), "#,##0.00")
.ActiveDocument.Bookmarks("total").Select
.Selection.Text = Format(CDbl(Forms!frmpo!amount), "#,##0.00")
End With

MergeButton_Err:
' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
Exit Sub

End Sub






Re: Put access fields in specific word document places

Pdaaddic

The above code works fine, but I'd like to export a table of data rather than just a single record.

I found this code, but unfortuately it appears to be for a format older than Office 2003 and as such doesn't appear to work:

Private Sub cmdPrint_Click()
'Print customer slip for current customer.
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rst As ADODB.Recordset
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
'Populate recordset object.
Set rst = New ADODB.Recordset
rst.Open Me.RecordSource, CurrentProject.Connection
'Cycle through records to fill Word form fields.
Do While Not rst.EOF
Set doc = appWord.Documents.Open("C:\WordForms\CustomerSlip.doc", , True)
With doc
.FormFields("fldCustomerID").Result = rst!CustomerID
.FormFields("fldCompanyName").Result = rst!CompanyName
.FormFields("fldContactName").Result = rst!ContactName
.FormFields("fldContactTitle").Result = rst!ContactTitle
.FormFields("fldAddress").Result = rst!Address
.FormFields("fldCity").Result = rst!City
.FormFields("fldRegion").Result = rst!Region
.FormFields("fldPostalCode").Result = rst!PostalCode
.FormFields("fldCountry").Result = rst!Country
.FormFields("fldPhone").Result = rst!Phone
.FormFields("fldFax").Result = rst!Fax
.Visible = True
.Activate
'.PrintOut
'.SaveAs "'" & rst!CustomerID & "'"
rst.MoveNext
End With
Loop
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub

Can anyone advise me on how to rectify this, as I feel I am SOOOO close.

Thanks

Adam