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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add MS Word letter to Access Form

Status
Not open for further replies.

HacH

Programmer
Dec 24, 2002
27
GB
Hi,

I'm not sure if this is possible in MS Access but any help would be much appreciated. I have created a simple form for Access. It contains contact info e.g. name address, email, telephone no etc.

I know it is possible to create a mailmerge in Word. However, I was wondering if it is possible to click on a button on my Access form, which will automatically lead MS Word with a pre-defined letter and the contact details from the current record?

Many Thanks for you help in advance

Mamoon
 
Yes, you can. Here is one FAQ:
Opening a Word Document and filling it with data from an Access Form
faq702-2379

You will find several posts if you look for Word and Bookmark.

You can also create a new letter and simply add fields from your form, without bookmarks:

[tt]wdLtr.Selection.TypeText Text:=CStr(Nz(Me.Title, "Dr")) & " " & _
CStr(Me.Firstname & " ") & CStr(Me.Lastname & " ") & vbNewLine[/tt]
 
Remou,

Thanks very much for your help on this. This may sound a little dumb but where do I put this code, into a module?

Thank you

Mamoon
 
The line of code I included is simply a hint, it is no use as it stands. The code in the FAQ would go in a module. You would then call it from your code.
 
Hi,

Sorry about the confusion. I have read the other thread (FAQ702-2379) and have followed all the instructions.

If you look at this thread, at the start there is some code, which creates the Word document and adds the text into the bookmarks. I have have made the bookmarks etc in the word docment and created the button and on the Event - OnClick property in my database. I have created a procedure with the following line of code:

CreateWordLetter "<path to your document>"

I have put this into the code window.

However, I am not sure where to put the following code?

Public Function CreateWordLetter(strDocPath As String)

'function returns nothing, but I created this as a
'function so all those macro users out there could
'use it also. :p
'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
Dim PrintResponse
Set dbs = CurrentDb

'create reference to Word Object

Set objWord = CreateObject("Word.Application")

'Word Object is created - now let's fill it with data.

With objWord
.Visible = True
.Documents.Open(strDocPath)
'move to each bookmark, and insert correct text.
.ActiveDocument.Bookmarks("<bookmark name>".Select
.Selection.Text=(Cstr(Forms!<form name>!<field
name>))
.ActiveDocument.Bookmarks.Add Name:=<bookmark name>,
Range = Selection.Range

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

Could you please explain if this code is meant to go into a macro/module or should I simply add it to the button's onclick property?

Many Thanks for your help

Mamoon
End With

'find out if the user would like to print the document
'at this time.

PrintResponse = MsgBox("Print this document?", vbyesno)
If PrintResponse = vbYes Then
objWord.ActiveDocument.PrintOut Background:=False
End If

'release all objects

Set objWord = nothing
Set dbs = nothing

End Function

 
The function CreateWordLetter is best in a module, as it is intended to be used by many forms.
 
Remou,

Many thanks for your help on this. It's all working now.

Thanks very much

Mamoon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top