473,418 Members | 2,177 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,418 software developers and data experts.

Merge multiple Word printouts from Access database into one

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.dot) 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.Application
Set objWord = New Word.Application
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.Documents.Add "C:\Template.dot"
objWord.Visible = True

If Not IsNull(rs!Field1) Then
objWord.ActiveDocument.Bookmarks.Item("bkfield1"). Range.Text = rs!Field1
End If
If Not IsNull(rs!Field2) Then
objWord.ActiveDocument.Bookmarks.Item("bkfield2"). Range.Text = rs!Field2
End If
If Not IsNull(rs!Field3) Then
objWord.ActiveDocument.Bookmarks.Item("bkfield3"). Range.Text = rs!Field3
End If
If Not IsNull(rs!Field4) Then
objWord.ActiveDocument.Bookmarks.Item("bkfield4”). Range.Text = rs!Field4
End If
If Not IsNull(rs!Field5) Then
objWord.ActiveDocument.Bookmarks.Item("bkfield5"). Range.Text = rs!Field5
End If

rs.MoveNext

Loop

Set rs = Nothing
Set objWord = Nothing
Aug 22 '07 #1
3 3053
FishVal
2,653 Expert 2GB
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.dot) 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.doc" mentioned below supposed to have bookmark "MergePosition" at the end.
Expand|Select|Wrap|Line Numbers
  1. Public Sub MergeMultipleDocumentCopies()
  2.  
  3.     Dim appWord As Word.Application
  4.     Dim docDocument As Word.Document
  5.  
  6.     Set appWord = CreateObject("Word.Application")
  7.  
  8.     With appWord
  9.         Set docDocument = .Documents.Add
  10.         .Visible = True
  11.         .Selection.InsertFile _
  12.             FileName:="F:\mibd\Access\Samples\Docs\Template.doc", _
  13.             Link:=False, Attachment:=False
  14.     End With
  15.  
  16.     For i = 1 To 4
  17.         With docDocument
  18.             .Bookmarks("MergePosition").Select
  19.             appWord.Selection.InsertFile _
  20.                 FileName:="X:\Template.doc", _
  21.                 Link:=False, Attachment:=False
  22.         End With
  23.     Next i
  24.  
  25.     Set docDocument = Nothing
  26.     Set appWord = Nothing
  27.  
  28. End Sub
  29.  
  30.  
Hope this makes sence.
Aug 23 '07 #2
Hi, there.

Here is an example how to merge multiple (5 in the example) copies of Word document.
Word document "Template.doc" mentioned below supposed to have bookmark "MergePosition" at the end.
Expand|Select|Wrap|Line Numbers
  1. Public Sub MergeMultipleDocumentCopies()
  2.  
  3.     Dim appWord As Word.Application
  4.     Dim docDocument As Word.Document
  5.  
  6.     Set appWord = CreateObject("Word.Application")
  7.  
  8.     With appWord
  9.         Set docDocument = .Documents.Add
  10.         .Visible = True
  11.         .Selection.InsertFile _
  12.             FileName:="F:\mibd\Access\Samples\Docs\Template.doc", _
  13.             Link:=False, Attachment:=False
  14.     End With
  15.  
  16.     For i = 1 To 4
  17.         With docDocument
  18.             .Bookmarks("MergePosition").Select
  19.             appWord.Selection.InsertFile _
  20.                 FileName:="X:\Template.doc", _
  21.                 Link:=False, Attachment:=False
  22.         End With
  23.     Next i
  24.  
  25.     Set docDocument = Nothing
  26.     Set appWord = Nothing
  27.  
  28. End Sub
  29.  
  30.  
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!
Aug 23 '07 #3
FishVal
2,653 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. Public Sub MergeMultipleDocumentCopies()
  2.  
  3.     Dim appWord As Word.Application
  4.     Dim docDocument As Word.Document
  5.  
  6.     Set appWord = CreateObject("Word.Application")
  7.  
  8.     With appWord
  9.         Set docDocument = .Documents.Add
  10.         .Visible = True
  11.         .Selection.InsertFile _
  12.             FileName:="X:\Template.doc", _
  13.             Link:=False, Attachment:=False
  14.     End With
  15.  
  16.     For i = 1 To 4
  17.         With docDocument
  18.             .Bookmarks("MergePosition").Select
  19.             appWord.Selection.InsertFile _
  20.                 FileName:="X:\Template.doc", _
  21.                 Link:=False, Attachment:=False
  22.         End With
  23.     Next i
  24.  
  25.     Set docDocument = Nothing
  26.     Set appWord = Nothing
  27.  
  28. End Sub
  29.  
  30.  
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.doc" 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.doc"
  • start loop iterating through records while not eof
  • drop field values on the bookmarks - this operation delete bookmark BTW
  • merge "X:\Template.doc" to merge position bookmark, so you have document with bookmarks set ready for next iteration
  • close loop

I hope this makes a sence.
Aug 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
9
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...
3
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...
4
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...
4
by: lesperancer | last post by:
I have 3 tables (office97) tblQuote quoteNbr tblDetails ( quote : 1 <-> M: quoteDetails) quoteNbr detailLine product value
1
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...
1
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...
8
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...
6
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...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
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...
0
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...

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.