Hi everyone,
I’m fairly new to Access and VBA, but I have set up the following code to export all the records in my database to a word template (C:\Template.do t) with bookmarks (bkfield1, bkfield2, etc.). Of course, when I run this code, each record prints out in a separate Word document. Is there any way for all the records to print out in a single Word document (i.e. to merge all the documents into one?)?
Thanks in advance for your help!
Dim objWord As Word.Applicatio n
Set objWord = New Word.Applicatio n
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT [Table].[Field1], [Table].[Field2], [Table].[Field3], [Table].[Field4], [Table].[Field5] FROM [Table] ORDER BY [Table].[Field1] DESC, [Table].[Field2];"
rs.Open strSQL, CurrentProject. Connection
Do While Not rs.EOF
objWord.Documen ts.Add "C:\Template.do t"
objWord.Visible = True
If Not IsNull(rs!Field 1) Then
objWord.ActiveD ocument.Bookmar ks.Item("bkfiel d1").Range.Te xt = rs!Field1
End If
If Not IsNull(rs!Field 2) Then
objWord.ActiveD ocument.Bookmar ks.Item("bkfiel d2").Range.Te xt = rs!Field2
End If
If Not IsNull(rs!Field 3) Then
objWord.ActiveD ocument.Bookmar ks.Item("bkfiel d3").Range.Te xt = rs!Field3
End If
If Not IsNull(rs!Field 4) Then
objWord.ActiveD ocument.Bookmar ks.Item("bkfiel d4”).Range.Text = rs!Field4
End If
If Not IsNull(rs!Field 5) Then
objWord.ActiveD ocument.Bookmar ks.Item("bkfiel d5").Range.Te xt = rs!Field5
End If
rs.MoveNext
Loop
Set rs = Nothing
Set objWord = Nothing
3 3065 FishVal 2,653
Recognized Expert Specialist
Hi everyone,
I’m fairly new to Access and VBA, but I have set up the following code to export all the records in my database to a word template (C:\Template.do t) with bookmarks (bkfield1, bkfield2, etc.). Of course, when I run this code, each record prints out in a separate Word document. Is there any way for all the records to print out in a single Word document (i.e. to merge all the documents into one?)?
Thanks in advance for your help!
Hi, there.
Here is an example how to merge multiple (5 in the example) copies of Word document.
Word document "Template.d oc" mentioned below supposed to have bookmark "MergePosit ion" at the end. -
Public Sub MergeMultipleDocumentCopies()
-
-
Dim appWord As Word.Application
-
Dim docDocument As Word.Document
-
-
Set appWord = CreateObject("Word.Application")
-
-
With appWord
-
Set docDocument = .Documents.Add
-
.Visible = True
-
.Selection.InsertFile _
-
FileName:="F:\mibd\Access\Samples\Docs\Template.doc", _
-
Link:=False, Attachment:=False
-
End With
-
-
For i = 1 To 4
-
With docDocument
-
.Bookmarks("MergePosition").Select
-
appWord.Selection.InsertFile _
-
FileName:="X:\Template.doc", _
-
Link:=False, Attachment:=False
-
End With
-
Next i
-
-
Set docDocument = Nothing
-
Set appWord = Nothing
-
-
End Sub
-
-
Hope this makes sence.
Hi, there.
Here is an example how to merge multiple (5 in the example) copies of Word document.
Word document "Template.d oc" mentioned below supposed to have bookmark "MergePosit ion" at the end. -
Public Sub MergeMultipleDocumentCopies()
-
-
Dim appWord As Word.Application
-
Dim docDocument As Word.Document
-
-
Set appWord = CreateObject("Word.Application")
-
-
With appWord
-
Set docDocument = .Documents.Add
-
.Visible = True
-
.Selection.InsertFile _
-
FileName:="F:\mibd\Access\Samples\Docs\Template.doc", _
-
Link:=False, Attachment:=False
-
End With
-
-
For i = 1 To 4
-
With docDocument
-
.Bookmarks("MergePosition").Select
-
appWord.Selection.InsertFile _
-
FileName:="X:\Template.doc", _
-
Link:=False, Attachment:=False
-
End With
-
Next i
-
-
Set docDocument = Nothing
-
Set appWord = Nothing
-
-
End Sub
-
-
Hope this makes sence.
Thanks for your reply, FishVal! I'm just confused about the file you refer to in line 12 of your code. Is this an existing file or a file that is created from the database?
What I would like to do is have all the records in my database print out into a single Word file. In other words, I would like the merging to take place before a separate file is created for each. Does this code do that? I tried to work with it in my database, but since I don't understand line 12, I wasn't able to manipulate it well.
Thank you so much for your help. Sorry if I seem like a dunce, but I'm very new to VBA and Access!
FishVal 2,653
Recognized Expert Specialist
Thanks for your reply, FishVal! I'm just confused about the file you refer to in line 12 of your code. Is this an existing file or a file that is created from the database?
What I would like to do is have all the records in my database print out into a single Word file. In other words, I would like the merging to take place before a separate file is created for each. Does this code do that? I tried to work with it in my database, but since I don't understand line 12, I wasn't able to manipulate it well.
Thank you so much for your help. Sorry if I seem like a dunce, but I'm very new to VBA and Access!
Hi, there.
Surely the code should look like. -
Public Sub MergeMultipleDocumentCopies()
-
-
Dim appWord As Word.Application
-
Dim docDocument As Word.Document
-
-
Set appWord = CreateObject("Word.Application")
-
-
With appWord
-
Set docDocument = .Documents.Add
-
.Visible = True
-
.Selection.InsertFile _
-
FileName:="X:\Template.doc", _
-
Link:=False, Attachment:=False
-
End With
-
-
For i = 1 To 4
-
With docDocument
-
.Bookmarks("MergePosition").Select
-
appWord.Selection.InsertFile _
-
FileName:="X:\Template.doc", _
-
Link:=False, Attachment:=False
-
End With
-
Next i
-
-
Set docDocument = Nothing
-
Set appWord = Nothing
-
-
End Sub
-
-
The code just give a hint how to perform document copies merge
Your code supposed to do the following.
Required: a template document "X:\Template.do c" with all your bookmarks defining where to drop field values and one additional bookmark defining where to merge the next record
Code logic - open new document and insert to the document start template document "X:\Template.do c"
- start loop iterating through records while not eof
- drop field values on the bookmarks - this operation delete bookmark BTW
- merge "X:\Template.do c" to merge position bookmark, so you have document with bookmarks set ready for next iteration
- close loop
I hope this makes a sence.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Traci |
last post by:
I need to do a mail merge letter from my database. The letter will be addressed
to small companies and in the body of the letter I need to list employees of the
company. There will be from 1 to 15 employees. My database is all set up to
provide the names and addresses of the companies and the list of employees of
each company. I need help on how to do the employee merge field when there will
be multiple employees to be listed in this field....
|
by: Neil Ginsberg |
last post by:
I have a strange situation using Access to automate a Word mail merge. Using
Access 2000 and Word 2000, the code opens Word, opens the document in Word,
sets a table
in the calling Access application as the data source, and then performs a
merge. Everything works fine.
However, when a user uses it in Access 2002 and Word 2002, an extra instance
of the Access application is opened and remains open at the end. Sometimes
it remains open
|
by: Andy Davis |
last post by:
I have set up a mail merge document in Word 2003 which gets its data from my
Access 2000 database. I want to set up a button on a form that:
1. runs the query to provide the dat for the merge document in Word;
2. opens the document and runs the merge process for the new data.
I have managed to write the code to perform step 1 ok, but I'm having
trouble with step 2. It opens the word document fine but does not perform
the mail merge of...
|
by: pmhaupt2 |
last post by:
I developed an Access 2003 program that will allow the user to produce
a group of Word letters that merge with data records from an Access
database.
I created a mail merge Word document and associated it with a specific
query in my Access database.
My problem is that when I run my "create letters" button from an Access
form, the program opens up Word correctly with the proper letter.
However, the MailMerge toolbar in Word appears...
|
by: lesperancer |
last post by:
I have 3 tables (office97)
tblQuote
quoteNbr
tblDetails ( quote : 1 <-> M: quoteDetails)
quoteNbr
detailLine
product
value
| |
by: law |
last post by:
I love the super easy word merge but I have a question. In my database
I select multiple records which I then want to merge to a single
document in word. I use the single word merge and get one of the
items. If I use the merge all button I get those I selected but they
are all on separate pages. Is there any way to modify the code so it
does not create a continuous section break after or before each record?
Thanks.
|
by: ashkash |
last post by:
I have an access database which takes information from a user and then uses the mail merge functionality to merge the data into a word document. I have a lot of subforms in the access database which allows the user to keep entering data if they need to. By this I mean each subform is like a dynamic table (continous form view) and the user can keep entering data if needed. For example, one of the subforms asks the user to enter the names of...
|
by: Ron B |
last post by:
Help!!!
What am I doing wrong? I am working with Office 2003 and am trying to create a command button on an Access form that will create a mail merge in Word from an Access table.
I want to make the mail merge seamless to the user so all they have to do is click on the command button and the letters are generated.
For the button I created an event procedure with the following code:
Private Sub RunLetters_Click()
|
by: crealesmith |
last post by:
Firstly, I have no problem with mail merging to Word, VB code for that
works perfectly.
On one mail merge I need to merge 15 fields of data that are from 3
seperate records.
The 3 records are all in the same table. If I use a control source that
is selecting the 3 records, all of the data is shown but spread over 3
pages in the mail merge, but needs to be listed together in one
paragraph.
Is there anyway of looping through the 3...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |