transferring data to a word document

  • Thread starter Thread starter Rani
  • Start date Start date
There are a couple of ways to go about this. My preference is to use OLE
automation to control Word and fill bookmarks in a template (.DOT file).
Some code that you can play with to familiarize yourself with the concepts
is found below.

First, create a Word template for your form letter. For this example, the
document name is C:\FormLetter.dot. In this template, you'll use the Insert
\ Bookmark command to create a bookmark at each location in the text where
you'll want to insert Access data. For this example,

I'm using simple Name and Address fields. In Access, open any Module in
design view. Then select Tools|References and make sure that you have a
reference to the appropriate Word library. Look for "Microsoft Word xx.x
Object Library, where xx.x is the Word version number. For this example,
you will also need a reference to DAO.

' Behind a command button on a form, insert the following code.

BEGIN CODE SAMPLE:

Dim objWord As Object

' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "c:\FormLetter.dot"
objWord.Visible = True

' The following code is generated from your Access form while it is
' manipulating an instance of Word.
' So, while it looks somewhat like VBA, it's really Word VBA.

If objWord.ActiveDocument.Bookmarks.Exists("FirstName") = True Then
objWord.ActiveDocument.Bookmarks("FirstName").Range.Text = me!FirstName
End If

If objWord.ActiveDocument.Bookmarks.Exists("LastName") = True Then
objWord.ActiveDocument.Bookmarks("LastName").Range.Text = me!LastName
End If

' ... continue reading data from form and inserting into bookmarks

END CODE SAMPLE

If the data you want inserted in your Word document is tabular, there is an
excellent code routine named CreateTableFromRecordset, created by Ken Getz
and his fellow authors of "The Access xx.x Developer's Handbook (xx.x is
version number). This routine writes the tabular output of a recordset
with multiple records to a table in Word. If you do not have a copy of this
book, do a Google search using CreateTableFromRecordset as the criterion to
get further information.
 
Thanks it really helped.
I do have another question though,
I have a an option group that replaces values on the form.
AKA if option group =1 then dollar values are being displayed, if option
group =2 then euro values are being displayed.
since the document is of a contract type it is very meaningful, how do I
reflect it on the document ?
another question , is there a way to open word print the current document
and close word automatically without displaying anything to the user ?
thanks
 
additional thing to the second question posted earlier - is pass a value to
an access table that the contract was printed at a certain date.

thanks
 
And one last question
is how do I limit the number of the decimal places, the number of decimal
places is set to 3 but while transferred to word it displays 16 decimal
places, how do I limit the number ?
 
After all of theWord Automation code, presuming there is a field on your
form for it, simply insert something like this:

Me!DatePrinted = Date()
 
With Word automation, I think you have to explicitly pass the formatted
value to word, even though it may be formatted quite nicely on your form and
in your table. You might try this:

If objWord.ActiveDocument.Bookmarks.Exists("Amount") = True Then
objWord.ActiveDocument.Bookmarks("Amount").Range.Text =
FormatCurrency(Me!Amount, 2)
End If
 
Rani,
I have a an option group that replaces values on the form.
AKA if option group =1 then dollar values are being displayed, if option
group =2 then euro values are being displayed.
since the document is of a contract type it is very meaningful, how do I
reflect it on the document ?

I have never worked with Euros, so I cannot provide a quick answer as to how
to get a value to format in Euros. However, the following code can be used
to evaluate the option group:

If objWord.ActiveDocument.Bookmarks.Exists("Amount") = True Then
If optCurrencyType = 1 then
objWord.ActiveDocument.Bookmarks("Amount").Range.Text =
FormatCurrency(Me!Amount, 2)
Else
objWord.ActiveDocument.Bookmarks("Amount").Range.Text = <some format
for euros here>
End If
End If

As to making the entire Word application invisible, here is a link to an
article on how to do that.

http://www.mvps.org/word/FAQs/InterDev/MakeAppInvisible.htm

Personally, my users want to view their documents before they print, so I
have never used that capability.

hth,
 
Format currency controls money.
what controls numbers ? AKA
I would like to limit the decimal place of a numeric field, how do I do that
?

thanks
 
In Access 2002, which I use most of the time, there is:

FormatNumber([MyNumber], 2)

where the argument after [MyNumber] denotes decimal places.

There is also:

Format([MyNumber], "###0.00")


hth,
 
If you were to use XpertDoc to do your merges, you could format your
numbers in the same manner as described by Cheryl but directly from
your MS-Word template.

You can check XpertDoc at www.xpertdoc.com


Cheryl Fischer said:
In Access 2002, which I use most of the time, there is:

FormatNumber([MyNumber], 2)

where the argument after [MyNumber] denotes decimal places.

There is also:

Format([MyNumber], "###0.00")


hth,
--
Cheryl Fischer
Law/Sys Associates
Houston, TX

Rani said:
Format currency controls money.
what controls numbers ? AKA
I would like to limit the decimal place of a numeric field, how do I do that
?

thanks

use
OLE example,
the the
Insert Module
in have
a Word
xx.x there
is
an Ken
Getz (xx.x
is of
this criterion
to
 
Back
Top