By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,325 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,325 IT Pros & Developers. It's quick & easy.

Print merged doc from Access

P: n/a
I have vba code calling a word document and printing it. I need to
merge some data from an access table to the word document before it
prints. This needs to happen for a large list, so the program figures
out which doc it needs and merges the data to the doc and prints it,
then go to the next record and does the same. How can I make this
happen?

Dec 6 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The follwoing code should be helpful.

To call the function:

MergeQuery "qryName", "C:\folder\MyWord.doc"

This will print one "Fill-in-form" or many form letters, depending on your
needs.

To call the function and add a photo to the Word Doc (only one at a time):

MergeQuery "qryName", "C:\folder\MyWord.doc", _
"C:\photoFolder\JimWebb.jpg", 360, 44, 140, 178

If you place a <ffin the document, a Form Field will be created (in case
there are fields to still be filled in by the user).

Filling in checkboxes is complicated - much easier to show than explain -
but essentially you need to use if/then in the mergefield, as in:

{IF {MERGEFIELD "is_felon"}="0" "O" "@"} Where the "O" is an empty checkbox
from wingdings and the "@" is a filled checkbox from wingdings.

Of course, this is not typed in text, but is created using the "Toggle Field
Codes" context menu choice in Word.

'************************************************* ******
' Much of this code section was adapted from code
' created by Albert D. Kallal. It has been adapted for this specific
' purpose. For why this method is preferable, and how to put it into your
' a application, please go to the original source at
' http:/www.members.shaw.ca/AlbertKallal/wordmerge/page2.html
'************************************************* ******
' The file, merge.txt, will be created automatically.
'
'************************************************* ******
Public Function MergeQuery(queryName As String, _
docName As String, Optional photoPath = "", _
Optional pLeft = 0, Optional pTop = 0, _
Optional pWidth = 0, Optional pHeight = 0) As
Boolean
On Error GoTo handle_error
MergeQuery = False

Dim i As Integer
Dim mWord As Object
Dim mDoc As Object
Set mWord = CreateObject("Word.Application")

DoCmd.Hourglass True
DoCmd.TransferText acExportDelim, , queryName, _
"C:\folder\merge.txt", True
With mWord
Set mDoc = .Documents.Open(docName)
.ActiveDocument.MailMerge.MainDocumentType = 0
If .ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
.ActiveDocument.Unprotect
End If

.ActiveDocument.MailMerge.OpenDataSource Name:="C:\folder\merge.txt",
_
AddToRecentFiles:=False, Format:=0, Connection:="", _
SQLStatement:="", SQLStatement1:=""
.ActiveDocument.MailMerge.Execute

If Len(photoPath) 0 Then
On Error Resume Next
.ActiveDocument.Shapes.AddPicture photoPath, False, _
True, 360, 44, 140, 178
On Error GoTo handle_error
End If

.Application.Selection.Find.ClearFormatting
Do While .Application.Selection.Find.Execute(FindText:="<ff >", _
Wrap:=wdFindContinue, Forward:=True) = True
.ActiveDocument.FormFields.Add
Range:=.Application.Selection.Range, _
Type:=wdFieldFormTextInput
Loop

If .ActiveDocument.ProtectionType = wdNoProtection Then
.ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=""
End If
.Application.Visible = True

mDoc.Close
End With
MergeQuery = True
exit_point:
Set mWord = Nothing
Set mDoc = Nothing
DoCmd.Hourglass False
Exit Function
handle_error:
MsgBox Err.Description
GoTo exit_point
End Function

--
Darryl Kerkeslager
<tr******@hotmail.comwrote
>I have vba code calling a word document and printing it. I need to
merge some data from an access table to the word document before it
prints. This needs to happen for a large list, so the program figures
out which doc it needs and merges the data to the doc and prints it,
then go to the next record and does the same. How can I make this
happen?

Dec 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.