Access records into word template

spike250

Registered User.
Local time
Today, 19:05
Joined
Aug 6, 2009
Messages
70
Hi,

I have been searching through google to find an answer to my problem with no luck so far so I hope someone will be able to help me or point me in the right direction.

Problem;
In access I have 10 different reports set up, so that when users enter data and click print, access then runs through a query to select which report to print. The problem with this is that every time a change is required i have to update multiple databases - What I would like to have is word templates stored on a network drive and when the users click print it exports the corresponding data into certain positions within the template and save it with a certain file name so that the template remains empty and also print a couple of copies.

Sorry for the length of this post.

I look forward to hearing from someone.

Many thanks

Spike:banghead:
 
You can do this with some VBA code. What you need beforehand is to create the template file by planting "bookmarks" to where you want the data. You can plant bookmarks using the insert/bookmarks menu. You then save this file as a template, to that network folder you've mentioned, and note down the full path to that file in notepad.

Afterwards you have to write code and tie that code to a button on the appropriate form. The code would be something like:
Code:
Public Sub FillWordBookmarks()

Dim appWord As Object
Set appWord = CreateObject("Word.Application")

Dim strPathToTemplateFile As String 
strPathToTemplateFile = "\\yourServer\yourFolder\YourTemplate.dotx"
Dim strPathToProspectiveFile As String
Dim strPreferredFileName 'fill this variable somewehere along the way
strPathToProspectiveFile = "C:\LocalFolder\" & strPreferredFileName & ".dotx"

appWord.Documents.Add strPathToTemplateFile 'opens the template as Document1
appWord.Visible = True

'Replace bookmarks with record data
With appWord.ActiveDocument.Bookmarks
    ![BookmarkName1].Range.Text = Me!CustomerAdress 'or something
    ![BookmarkName2].Range.Text = Forms![Orders]![OrderID] ' or sth
...
strPreferredFileName = Me.CustomerName 'or something
...
appWord.ActiveDocument.SaveAs strPathToProspectiveFile 
appWord.ActiveDocument.Close
appWord.Quit
End Sub
if you play around with the code, eventually you'll modify it to suit your needs.
 

Users who are viewing this thread

Back
Top Bottom