Link to home
Start Free TrialLog in
Avatar of sata
sata

asked on

Reading the values of a datasource

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?
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
something like

 .DataSource.LastRecord = x

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