473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Print merged doc from Access

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
1 1886
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Piotr | last post by:
Is there any way to split all merged words but www and e-mail addresses? I have regexp preg_replace("/(\.)(])/", "\\1 \\2", "www.google.com any,merged.words mymail@domain.com") it give me...
1
by: NeilAnderson | last post by:
Hi Could anybody point me in the direction of some info about emailing merged word word documents. I have a word document that is merged with a query which I want to email to about seventy...
2
by: Dean Slindee | last post by:
Anybody written code in VB.NET to: 1) show a print preview window of reports already written and stored in an Access 2002 database; or 2) execute the print of a report stored in an Access 2002...
0
by: marfi95 | last post by:
Does anyone know if its possible using the standard controls shipped with vb.net, if its possible to create a listview with merged cells. By merged cells, I'm referring to basically what can be...
0
by: Steven Bolard | last post by:
Hi Im using .net 2.0 and i am merging a table from one strongly typed dataset into another. If i then do a select on the rows within the table I merged and subsequently try to access the columns...
1
by: itm | last post by:
I have merged the data from Access. Now I need Word to start a new page every time the sequence number changes. How is this accomplished? Also, is it possible to tell Word to hide duplicates? ...
1
by: MAdcock | last post by:
Hi, I have written some code to create a copy workbook of another sheet in a workbook, but the outcome is that the copy has links (='Sheet1'!A5) for example instead of the values contained within...
2
by: riwar | last post by:
Hello. Anybody knows how to clear merged cells using c#? If i try to do this via ...Cells.get_Range(x,x).ClearContents(); compiler say that there is no possibility to operate with this method on...
1
by: mymoup | last post by:
hi, all. I want to verify data merged from access in sqlserver.but it has too many records .so i need a tool do this. someone maybe has it. thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.