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?
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:
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
see also the last post one this thread
http://www.vbaexpress.com/forum/showthread.php?t=76&page=1
http://www.vbaexpress.com/forum/showthread.php?t=76&page=1
.DataSource.LastRecord = x
For i = 1 To x
.Execute Pause:=True
.DataSource.ActiveRecord = wdNextRecord
Next i