Solved

Reading the values of a datasource

Posted on 2004-09-14
3
591 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
ID: 12061632
for the next record you could try

.DataSource.ActiveRecord = wdNextRecord
0
 
LVL 44

Expert Comment

by:bruintje
ID: 12061638
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
ID: 12061648
see also the last post one this thread
http://www.vbaexpress.com/forum/showthread.php?t=76&page=1
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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