View Single Post
  #1   Report Post  
Tlar
 
Posts: n/a
Default import data into word from excel or access

I have a similar issue. Doug I used your recommended code and it works
however it only takes one item from the listbox in my user form and inserts
it into my word textfield. HOw do I get all say 6 items listed in the
listbox of my user form to be inserted into a word doc form field? Can I go
userform listbox to word textfield?
thanks for all your great help.
Tlar

"Doug Robbins" wrote:

Use a userform in the template and use the following code in the initialize
event of the user form to populate a listbox on the userform with the names
and addresses of the individuals from your database:

Private Sub UserForm_Initialize()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
Set db = OpenDatabase("D:\Access\ResidencesXP.mdb")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT * FROM Owners")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
' Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing

End Sub

The following code in the click event of a command button on the user form
will get the information from the record that is selected in the listbox and
insert it into the document at the location identified by a bookmark that is
inserted into the template:

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.Insert After Addressee
UserForm2.Hide
End Sub

To see how to get started with a userform, see the article "How to create a
Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
"David" wrote in message
...
Hi there,

I want to be able to automatically fill in address information in a
template
when I type in the name of a person. Can someone help explain how I do
this?
A marco or something maybe? The info will be stored in excel spreadsheet
or
access....

thanks!!!!!

david