Mailmerge

J

Jason

Hi,

Can somebody help me with my code? I would like to do a mailmerge through
document properties, which i've created in word.

I'm using a continueous form in access and a commandbutton to do a
mailmerge.
I can manage it to do it for one record, but not for multiple records.

This is what i would to do:

- if someone wants to merge 1 record then the code should call word, by
using document properties or variables and create a document based on the
template.

-if all or a selection of records has been choosen then it should do like
above, but then reproduce the same document in one file (otherwise they have
to
print
all documents separatelately, rather then just press print).

Here's my code:

On Error GoTo ErrorHandler

Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim strTemplateDir As String
Dim prps As Object
Dim strDate As String

Set appWord = GetObject(, "Word.Application")
strDate = CStr(Date)

strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
strTemplateDir = strTemplateDir & "\"

'Debug.Print "Office templates directory: " & strTemplateDir

strLetter = strTemplateDir & "DocProps.dot"
'Debug.Print "Letter: " & strLetter

Set docs = appWord.Documents
docs.Add strLetter

Set prps = appWord.ActiveDocument.CustomDocumentProperties

'There should be a way to reference the prps in a for each control in my
form, but i don't know how to do it. So it did it like this.
With prps
.Item("TodayDate").Value = strDate
.Item("Address").Value = Nz(Me![txtAddress])
.Item("Salutation").Value = Nz(Me![txtSalutation])
.Item("CompanyName").Value = Nz(Me![txtCompanyName])
.Item("City").Value = Nz(Me![txtCity])
.Item("StateProv").Value = Nz(Me![txtStateOrProvince])
.Item("PostalCode").Value = Nz(Me![txtPostalCode])
.Item("JobTitle").Value = Nz(Me![txtTitle])
End With

With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With

ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If

So if anyone can adjust or improve my code, I would be very happy.
 
A

Anna S

Jason

I have used the code offered by Mr Kallal with great success. It is easy to
use - fantastic.
Cheers
Anna

And thanks Mr Kallal

Jason said:
Hi,

Can somebody help me with my code? I would like to do a mailmerge through
document properties, which i've created in word.

I'm using a continueous form in access and a commandbutton to do a
mailmerge.
I can manage it to do it for one record, but not for multiple records.

This is what i would to do:

- if someone wants to merge 1 record then the code should call word, by
using document properties or variables and create a document based on the
template.

-if all or a selection of records has been choosen then it should do like
above, but then reproduce the same document in one file (otherwise they have
to
print
all documents separatelately, rather then just press print).

Here's my code:

On Error GoTo ErrorHandler

Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim strTemplateDir As String
Dim prps As Object
Dim strDate As String

Set appWord = GetObject(, "Word.Application")
strDate = CStr(Date)

strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
strTemplateDir = strTemplateDir & "\"

'Debug.Print "Office templates directory: " & strTemplateDir

strLetter = strTemplateDir & "DocProps.dot"
'Debug.Print "Letter: " & strLetter

Set docs = appWord.Documents
docs.Add strLetter

Set prps = appWord.ActiveDocument.CustomDocumentProperties

'There should be a way to reference the prps in a for each control in my
form, but i don't know how to do it. So it did it like this.
With prps
.Item("TodayDate").Value = strDate
.Item("Address").Value = Nz(Me![txtAddress])
.Item("Salutation").Value = Nz(Me![txtSalutation])
.Item("CompanyName").Value = Nz(Me![txtCompanyName])
.Item("City").Value = Nz(Me![txtCity])
.Item("StateProv").Value = Nz(Me![txtStateOrProvince])
.Item("PostalCode").Value = Nz(Me![txtPostalCode])
.Item("JobTitle").Value = Nz(Me![txtTitle])
End With

With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With

ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If

So if anyone can adjust or improve my code, I would be very happy.
 
A

Albert D. Kallal

If you download my template and word merge system, then you can do what you
want:
I'm using a continueous form in access and a commandbutton to do a
mailmerge.
I can manage it to do it for one record, but not for multiple records.

For one record, the command to merge to word would be:

MergeSingleWord

The above then launches a word template manager. No hard coding of fields is
required here. Give my sample a try.
-if all or a selection of records has been choosen then it should do like
above, but then reproduce the same document in one file (otherwise they
have

You don't mention how you plan to select multiple records. I have a working
sample of how to select multiple records at the link below.

So, you could have a merge selected button, and it would simply go something
like:

dim strSql as string

strSql = "select * from tblCustomers where contactID in (" & MySelected &
")"

MergeAlWord (strSql)

So, with the above two libraries, you only need to write about 4 lines of
code to do what you want...

You can find the code here:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html
 
J

Jason

Hi Albert,

Thnx for the tip, but i want to learn so i can write it myself. Can you
adjust my code so i can learn how it should be done.

About selecting multiple records, i use a listbox or sometimes a form with
checkboxes.

I would be very happy if you could help me.

Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top