Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: Access Database into a Word Template...... 1

Status
Not open for further replies.

Sherly25

Programmer
Jun 2, 2003
15
US
Here is my problem:
I have 2 Access databases finished. But I need to transfer them into a 2 corresponding Microsoft Word templates. (Which were already created). I need each field to conrespond: for example on my Access database I has:
Job Number
To:
Date:
ect...
on the word template I have the same:
To:
Date:
Job No.
ext...
How do I link them in such a way, so that I can get the information from the Access Form into the Word Template?
If I need VB or SQL code, please let me know and send me the code...Please...I need to get this done and have been troubleshooting without success. I actaully have a VB program, but have an error and cannot fix it for I don't know what reason.
Your help would be gladly appreciated...
Thanks everyone!!
 
Open your database. Do a ALT+F11. Copy and paste the following in the VBA window:(change "form name", "field name", "bookmark name" to yours)

Public Function CreateWordLetter(strDocPath As String)

'if no path is passed to function, exit - no further
'need to do anything

If IsNull(strDocPath) Or strDocPath = "" Then
Exit Function
End If

Dim dbs As Database
Dim objWord As Object
Set dbs = CurrentDb

'create reference to Word Object

Set objWord = CreateObject("Word.Application")

'Word Object is created

With objWord
.Visible = True
.Documents.Open(strDocPath)

'move to each bookmark, and insert text.

.ActiveDocument.Bookmarks("bookmark name").Select
.Selection.Text=(Cstr(Forms![form name]![fieldname]))
.ActiveDocument.Bookmarks.Add Name:="bookmark name", Range = Selection.Range 'on one line

** continue the ActiveDocument and Selection statements for each bookmark that you have on the Word Document **

End With

'release all objects

Set dbs = nothing

End Function


1) Open your word document. If you do not have a current
document that you are not using, create a new one.

2) In the places where the values will change based on
information in the record, create a bookmark and name
it appropriately. i.e. If you have a field in Access
called FirstName, then make the bookmark name "First
Name", etc etc

3) After creating / editing your document, save it.
Remember the full path name for the file.

4) Create a command button on a form.

5) In the On_Click event of your button, click the build button, select code builder and click OK. Type the following:

CreateWordLetter "path to your document"

Neil




 
Thanks I have to change a couple of lines on the code but it worked!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top