Solved

Reading the values of a datasource

Posted on 2004-09-14
3
586 Views
Last Modified: 2006-11-17
Hi,

I am trying to do this prog that can activate mail merge on a doc with it's datasource defined. During the merge, I will send the doc into a virtual printer to convert it into pdf.

Dim objWord As Word.Application
Dim objDoc As Word.Document

Set objWord = New Word.Application
Set objDoc = objWord.Documents.Open("C:\Test4.doc", , False)

Dim x As Long
Dim i As Long
Dim s as String

 With objDoc.MailMerge
   
   .Destination = wdSendToPrinter
   .SuppressBlankLines = True
   
   'get the record count of the datasource
   With .DataSource
     .ActiveRecord = wdLastRecord
     x = .ActiveRecord
     'set the activerecord back to the first
     .ActiveRecord = wdFirstRecord
   
   End With
   
   'loop the datasource count and merge one record at a time
   For i = 1 To x
     .DataSource.FirstRecord = i
     .DataSource.LastRecord = i
XXXXXXXXXXXXXX
     .Execute Pause:=True
    Next i
 End With
 
 objDoc.Close (False)

I try to replace the "XXXXXXXXXXXXXX" line with .datasource.fields("ID") to retrieve the value of the "ID" for each record to save as my filename, however .datasource.fields("ID") always returns only the ID value of the first record in the datasource, although I have already change the id of the first record.

What can I do to get the values correctly?
0
Comment
Question by:sata
  • 3
3 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 125 total points
Comment Utility
for the next record you could try

.DataSource.ActiveRecord = wdNextRecord
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
something like

 .DataSource.LastRecord = x

For i = 1 To x
     .Execute Pause:=True
     .DataSource.ActiveRecord = wdNextRecord
Next i
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
see also the last post one this thread
http://www.vbaexpress.com/forum/showthread.php?t=76&page=1
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now