Reputation: 29
I have an Excel file with employee data and a Word template. I have mail merged and created a macro which asks me for the employee name, and based on the name, it fetches the employee details of that employee to the Word file from the Excel file.
The macro is able to retrieve records if the record comes from top to bottom of the Excel sheet. However, any record which is above the current retrieved record is not getting fetched. It's really a trouble. Could you please help?
Below is my code:
Sub getdata()
Dim numRecord As Integer
Dim myName As String
myName = InputBox("Enter Name:")
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:=myName, Field:="Name") = True Then
numRecord = dsMain.ActiveRecord
End If
End Sub
Upvotes: 1
Views: 472
Reputation: 84465
Here is a direct quote from the MailMergeDataSource.FindRecord Method documentation:
"The FindRecord method does a forward search only. Therefore, if the active record is not the first record in the data source and the record for which you are searching is before the active record, the FindRecord method will return no results. To ensure that the entire data source is searched, set the ActiveRecord property to wdFirstRecord ."
This, according to the MailMergeDataSource.ActiveRecord Property documentation will be something like:
With ActiveDocument.MailMerge
.DataSource.ActiveRecord = wdFirstRecord
End With
Upvotes: 2